Java Modifiers: Understanding Access Levels and Properties in Java Programming
Learn about Java modifiers, including access modifiers like public, private, and protected. This guide covers how modifiers define access levels for classes, methods, and attributes in Java programming, helping you write more efficient and secure code.
Java Modifiers
In Java, modifiers are keywords that define access levels and other properties of classes, attributes, methods, and constructors.
Access Modifiers
Access modifiers control the visibility of classes, attributes, methods, and constructors:
Modifier | Description |
---|---|
public | The class or code is accessible for all classes. |
private | The code is only accessible within the declared class. |
default | The code is accessible in the same package (when no access modifier is specified). |
protected | The code is accessible in the same package and subclasses. |
Non-Access Modifiers
Non-access modifiers provide other functionality and properties:
Modifier | Description |
---|---|
final | Attributes and methods cannot be overridden/modified (for attributes, it means they cannot have their values changed). |
static | Attributes and methods belong to the class rather than an object instance. |
abstract | Methods belong to an abstract class and do not have a body; instead, they are provided by subclasses. |
transient | Attributes and methods are skipped when serializing the object containing them. |
synchronized | Methods can only be accessed by one thread at a time. |
volatile | The value of an attribute is not cached thread-locally, and is always read from the main memory. |
Final
Use final
to prevent attributes from being modified:
Example
public class Main {
final int x = 10;
final double PI = 3.14;
public static void main(String[] args) {
Main myObj = new Main();
// myObj.x = 50; // Error: cannot assign a value to a final variable
// myObj.PI = 25; // Error: cannot assign a value to a final variable
System.out.println(myObj.x);
}
}
Static
Use static
to define methods that can be accessed without creating an object instance:
Example
public class Main {
// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
public static void main(String[] args) {
myStaticMethod(); // Call the static method
Main myObj = new Main(); // Create an object of Main
myObj.myPublicMethod(); // Call the public method
}
}
Abstract
Use abstract
for methods in abstract classes that do not have a body:
Example
// Main.java
abstract class Main {
public String fname = "John";
public int age = 24;
public abstract void study(); // abstract method
}
// Second.java
class Student extends Main {
public int graduationYear = 2018;
public void study() { // provide body for abstract method
System.out.println("Studying all day long");
}
}
// Second.java continued
class Second {
public static void main(String[] args) {
// create an object of the Student class
Student myObj = new Student();
System.out.println("Name: " + myObj.fname);
System.out.println("Age: " + myObj.age);
System.out.println("Graduation Year: " + myObj.graduationYear);
myObj.study(); // call abstract method
}
}
Modifiers in Java help control access levels and provide additional functionality to classes, methods, and attributes.