WikiGalaxy

Personalize

Java Modifiers

Access Modifiers

Access modifiers in Java control the visibility of classes, constructors, methods, and variables. They are essential for encapsulation and security.

Non-Access Modifiers

Non-access modifiers provide functionality other than access control, such as abstract, static, final, synchronized, etc.


public class Example {
  private String name;
  public int age;
  protected String address;
  String phoneNumber; // default access
}
    

Private Modifier

The private modifier is used to restrict access to the members of a class. Members declared private can only be accessed within the same class.

Public Modifier

The public modifier provides the highest level of access, allowing members to be accessible from any other class.

Console Output:

Access Modifiers Demonstrated

Static Modifier

Static Fields and Methods

Static fields and methods belong to the class rather than any specific instance, making them accessible without creating an object of the class.


public class MathUtils {
  public static double PI = 3.14159;
  
  public static double square(double number) {
    return number * number;
  }
}
    

Usage

Static members can be accessed directly using the class name, without needing an instance of the class.

Console Output:

PI: 3.14159

Final Modifier

Final Classes, Methods, and Variables

The final modifier is used to restrict inheritance and modification. A final class cannot be extended, a final method cannot be overridden, and a final variable cannot be reassigned.


public final class Constants {
  public static final double GRAVITY = 9.8;
  
  public final void display() {
    System.out.println("Display method");
  }
}
    

Benefits of Final Modifier

Using final helps in preventing unnecessary changes and ensures consistency in behavior across different parts of the application.

Console Output:

GRAVITY: 9.8

Abstract Modifier

Abstract Classes and Methods

Abstract modifier is used to declare a class that cannot be instantiated or a method that must be implemented by subclasses.


abstract class Animal {
  abstract void makeSound();
  
  public void eat() {
    System.out.println("Eating...");
  }
}
    

Purpose of Abstract Classes

Abstract classes provide a base for subclasses to build on, enforcing a contract for specific methods without providing a full implementation.

Console Output:

Abstract Class Example

Synchronized Modifier

Synchronization in Java

The synchronized modifier is used to control access to a method or block by multiple threads, ensuring that only one thread can execute it at a time.


public class Counter {
  private int count = 0;
  
  public synchronized void increment() {
    count++;
  }
}
    

Importance of Synchronization

Synchronization prevents thread interference and consistency problems, making it crucial for multi-threaded applications.

Console Output:

Synchronized Method Example

Transient Modifier

Transient Variables

The transient modifier is used in serialization to indicate that a field should not be serialized.


import java.io.Serializable;

public class User implements Serializable {
  private String username;
  private transient String password;
}
    

Use Case for Transient

Transient is useful when you want to prevent sensitive data from being serialized, such as passwords.

Console Output:

Password Field Not Serialized

Volatile Modifier

Volatile Variables

The volatile modifier is used to indicate that a variable's value will be modified by different threads.


public class SharedResource {
  private volatile boolean flag = true;
  
  public void toggleFlag() {
    flag = !flag;
  }
}
    

When to Use Volatile

Volatile ensures visibility of changes to variables across threads, making it essential for flags and status updates in multi-threaded environments.

Console Output:

Volatile Variable Example

Native Modifier

Native Methods

The native modifier is used to declare a method that is implemented in native code using JNI (Java Native Interface).


public class NativeExample {
  public native void print();
  
  static {
    System.loadLibrary("NativeLib");
  }
}
    

Purpose of Native Methods

Native methods are used for performance-intensive tasks and to access system-level resources unavailable in Java.

Console Output:

Native Method Example

Strictfp Modifier

Strictfp Keyword

The strictfp modifier is used to restrict floating-point calculations to ensure portability across platforms.


public strictfp class Calculator {
  public strictfp double add(double a, double b) {
    return a + b;
  }
}
    

When to Use Strictfp

Strictfp is beneficial when consistent floating-point results are needed across different Java platforms.

Console Output:

Strictfp Class Example

Interface Modifier

Interfaces in Java

An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types.


interface Animal {
  void makeSound();
  
  default void eat() {
    System.out.println("Eating...");
  }
}
    

Purpose of Interfaces

Interfaces are used to achieve abstraction and multiple inheritance in Java, allowing different classes to implement the same set of methods.

Console Output:

Interface Example

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025