
Java Modifiers in Java
Modifiers in Java
In Java, modifiers are keywords used to define the properties or behavior of classes, methods, variables, and other elements. Modifiers control access levels, inheritance, visibility, and other characteristics of Java entities. There are access modifiers and non-access modifiers.
1. Access Modifiers
Access modifiers control the visibility and accessibility of classes, methods, and variables.
Public
Visibility: The class, method, or variable is accessible from anywhere in the program, even from other packages.
Usage:
You can declare a class, method, or field as public when you want it to be accessible by all other classes.
public class MyClass { public int number; public void display() { System.out.println("Public method!"); }}
Private
Visibility: The class, method, or variable is accessible only within the same class. It cannot be accessed from other classes, even within the same package.
Usage:
Use
private
to hide sensitive or internal details of a class, preventing them from being modified externally.
public class MyClass { private int number; // Private variable public void setNumber(int number) { // Public method this.number = number; } public int getNumber() { return this.number; }}
Protected
Visibility: The class, method, or variable is accessible within the same package and by subclasses (even if they are in different packages).
Usage:
Use
protected
for inherited members, which you want to be accessible to subclasses.
public class ParentClass { protected int number; protected void display() { System.out.println("Protected method!"); }}public class ChildClass extends ParentClass { public void show() { System.out.println("Accessing protected number: " + number); }}
Default (Package-Private)
Visibility: The class, method, or variable is accessible only within the same package. If no access modifier is specified, it defaults to package-private.
Usage:
Use default access when you want members to be accessible only within the package.
class MyClass { int number; // Default access (no modifier) void display() { System.out.println("Default method!"); }}
2. Non-Access Modifiers
Non-access modifiers provide additional functionality, such as controlling inheritance and method behavior.
Static
Usage: The
static
modifier indicates that the field or method belongs to the class, rather than to instances of the class. Static members are shared among all instances of the class.Applies to: Variables, methods, blocks, and nested classes.
public class MyClass { static int count = 0; static void increment() { count++; }}
Final
Usage:
Variables: A
final
variable cannot be reassigned after initialization.Methods: A
final
method cannot be overridden by subclasses.Classes: A
final
class cannot be subclassed.
final class MyClass { // Cannot be extended final int number = 10; // Cannot be reassigned final void display() { // Cannot be overridden System.out.println("Final method"); }}
Abstract
Usage: The
abstract
modifier is used to declare a class or method that must be implemented by subclasses.Abstract classes cannot be instantiated directly.
Abstract methods are declared in the abstract class but have no implementation. Subclasses are required to implement these methods.
abstract class Animal { abstract void sound(); // Abstract method void breathe() { System.out.println("Breathing..."); }}class Dog extends Animal { @Override void sound() { System.out.println("Bark"); }}
Synchronized
Usage: The
synchronized
modifier is used to control access to a method or block of code by multiple threads. Only one thread can execute a synchronized method or block at a time.Applies to: Methods and blocks.
public class Counter { private int count = 0; public synchronized void increment() { // Ensures thread-safety count++; }}
Volatile
Usage: The
volatile
modifier ensures that a variable is always read from and written to main memory, rather than being cached in a thread’s local memory.It is useful in multi-threaded programming to ensure the consistency of shared variables.
public class SharedData { private volatile boolean flag = false; public void setFlag(boolean flag) { this.flag = flag; } public boolean getFlag() { return this.flag; }}
Transient
Usage: The
transient
modifier prevents a variable from being serialized. When an object is serialized (i.e., converted to a byte stream for storage or transmission),transient
variables are not included.
public class MyClass implements Serializable { private transient int tempData; // Will not be serialized private int permanentData; // Will be serialized}
Native
Usage: The
native
modifier indicates that a method is implemented in another language (e.g., C or C++) and is called from Java. These methods use the Java Native Interface (JNI).
public class MyClass { public native void myNativeMethod(); // Declaration of a native method static { System.loadLibrary("MyLibrary"); // Loading native library }}
Strictfp
Usage: The
strictfp
modifier restricts floating-point calculations to ensure portability. It ensures that floating-point calculations are consistent across platforms.Applies to: Classes, methods, and interfaces.
strictfp class MyClass { // Class with strict floating-point calculations double multiply(double a, double b) { return a * b; }}
3. Summary of Java Modifiers
Modifier | Type | Usage |
---|---|---|
public | Access Modifier | Accessible from anywhere |
private | Access Modifier | Accessible only within the same class |
protected | Access Modifier | Accessible within the same package and by subclasses |
default | Access Modifier | Accessible within the same package (no modifier) |
static | Non-Access | Belongs to the class, not instances |
final | Non-Access | Immutable or non-overridable |
abstract | Non-Access | Requires implementation in subclass, cannot instantiate abstract class |
synchronized | Non-Access | Ensures thread-safety in multi-threaded programming |
volatile | Non-Access | Ensures variable visibility across threads |
transient | Non-Access | Prevents variable from being serialized |
native | Non-Access | Specifies that method is implemented in native code (e.g., C/C++) |
strictfp | Non-Access | Ensures consistent floating-point calculations |
Conclusion
Modifiers in Java are essential tools for controlling the behavior and accessibility of classes, methods, variables, and other elements. They help with:
Controlling visibility and access levels (
public
,private
,protected
,default
)Defining the behavior of methods and variables (
static
,final
,abstract
, etc.)Enhancing thread safety (
synchronized
,volatile
), serialization (transient
), and portability (strictfp
)
Understanding and applying modifiers effectively is crucial for writing maintainable, efficient, and secure Java applications.