Java Modifiers
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Modifiers

Jeevadharshan

 Java Modifiers 

In Java, modifiers are keywords that define how classes, attributes, methods, and constructors can be accessed or behave. They are essential for implementing encapsulation, security, and proper object-oriented design. 

You’ve already seen the public keyword in many examples: 

public class Application 

Here, public is a modifier that controls visibility. 

Types of Java Modifiers 

Java modifiers are grouped into two main categories: 

1 .Access Modifiers 

Control who can access a class or member. 

2 .Non-Access Modifiers 

Control behavior or characteristics (e.g., static, final, abstract). 

Access Modifiers in Java

Class-Level Access Modifiers 

At the top level (for classes), only two access levels are allowed: 

Table

Modifier   
public     
Accessibility 
Accessible from any package 
default (no keyword)    
Accessible only within the same package

Member-Level Access Modifiers

For attributes, methods, and constructors, Java provides four access levels:

Table

Modifier  Same Class Same Package Subclass Other Packages 
 
public    ✔   ✔   ✔   ✔ 
protected  ✔    ✔   ✔  ✖ 
Default         ✔    ✔  ✖    ✖ 
private     ✔     ✖    ✖   ✖ 

Public vs Private — Practical Example 

This example demonstrates how access levels affect visibility. 
 
class Customer { 
    public String name = "Meera";     // accessible everywhere 
    private double balance = 2500.0;  // accessible only inside class 
 
    public void showBalance() { 
        System.out.println("Balance: " + balance); 
    } 
 
public class BankApp { 
    public static void main(String[] args) { 
        Customer c = new Customer(); 
 
        System.out.println(c.name);   // allowed 
        c.showBalance();              // allowed 
 
        // System.out.println(c.balance); // compile-time error 
    } 
}  

Output 

Meera 
Balance: 2500.0
  • balance is private, so it cannot be accessed directly outside Customer. 
  • Public methods provide controlled access — a core principle of encapsulation.

Java Non-Access Modifiers 

Non-access modifiers in Java define the behavior, lifecycle, and characteristics of classes, methods, and variables. Unlike access modifiers (public, private, etc.), they do not control visibility. Instead, they specify how program elements behave in inheritance, memory sharing, abstraction, and concurrency. 
 
The most commonly used non-access modifiers are:
  • final 
  • static 
  • abstract 
Java also provides advanced modifiers such as transient, synchronized, and volatile.

final Modifier in Java 

The final keyword prevents modification. Its effect depends on where it is applied:
  • final variable → constant (cannot change value) 
  • final method → cannot be overridden 
  • final class → cannot be inherited
Example: Final Variable (Constant) 
 
public class Product { 
    final double TAX_RATE = 0.18; 
 
    public static void main(String[] args) { 
        Product p = new Product(); 
        // p.TAX_RATE = 0.20; X compile-time error 
        System.out.println("Tax rate: " + p.TAX_RATE); 
    } 

Output 

Tax rate: 0.18

static Modifier in Java 

A static member belongs to the class itself, not to individual objects.
  • Shared across all objects 
  • Can be accessed without creating an instance
Example: Static Field and Method 
 
public class VisitorCounter { 
    static int totalVisitors = 0; 
 
    public VisitorCounter() { 
        totalVisitors++; 
    } 
 
    static void showCount() { 
        System.out.println("Visitors: " + totalVisitors); 
    } 
 
    public static void main(String[] args) { 
        new VisitorCounter(); 
        new VisitorCounter(); 
        new VisitorCounter(); 
 
        VisitorCounter.showCount(); 
    } 

Output 

Visitors: 3 
 
Static methods cannot directly access non-static (instance) variables.

abstract Modifier in Java 

The abstract keyword defines incomplete classes or methods that must be implemented by subclasses.
  • Abstract classes cannot be instantiated 
  • Abstract methods have no body 
  • Subclasses must provide implementation 
Example: Abstract Class and Method 
 
abstract class Payment { 
    double amount; 
 
    public Payment(double amount) { 
        this.amount = amount; 
    } 
 
    abstract void process();   // abstract method 
 
class CreditCardPayment extends Payment { 
 
    public CreditCardPayment(double amount) { 
        super(amount); 
    } 
 
    @Override 
    void process() { 
        System.out.println("Processing credit card payment: " + amount); 
    } 
 
public class BillingApp { 
    public static void main(String[] args) { 
        Payment p = new CreditCardPayment(1500); 
        p.process(); 
    } 
}  

Output 

Processing credit card payment: 1500.0

Non-Access Modifiers for Classes

Table

Modifier  Description 
final   Class cannot be extended 
abstract  
Class cannot be instantiated

final class Utility { }      // cannot be inherited 
abstract class Shape { }     // cannot be instantiated 

Non-Access Modifiers for Fields & Method

Table

Modifier  
final 
Applies To 
field, method 
Purpose 
Prevent modification/override 
static        
field, method 
Shared across objects 
abstract 
transient 
synchronized 
Volatile         
method   
field          
method   
field          
Must be implemented 
Skip during serialization 
Thread-safe access 
Always read from main memory 

Advanced Modifiers Overview 

transient 

Prevents a field from being serialized. 

class Session { 
   transient String password; 

synchronized 

Allows only one thread to execute a method at a time. 

public synchronized void updateBalance() { 
// thread-safe code 

volatile 

Ensures variable changes are visible across threads. 

volatile boolean running = true;
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !