codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

29 / 65 topics
24Java OOP25Java Classes/Objects26Java Class Attributes27Java Class Methods28Java Constructors29Java Modifiers30Java Encapsulation31Java Packages / API
Tutorials/Java Programming/Java Modifiers
☕Java Programming

Java Modifiers

Updated 2026-05-12
30 min read

Java Modifiers

In Java programming, modifiers are keywords that provide additional information about classes, methods, variables, etc. They help in defining the scope and behavior of these elements. Understanding modifiers is crucial for controlling access to your code components and ensuring encapsulation.

This tutorial will cover two types of modifiers: access modifiers (public, private, protected) and non-access modifiers (final, static, abstract). By the end of this lesson, you'll be able to effectively use these modifiers to manage the visibility and behavior of your Java classes and objects.

Introduction

Modifiers in Java play a vital role in defining how different parts of your code interact with each other. They ensure that your classes and methods are accessible only when intended, enhancing security and maintainability.

Access modifiers control the visibility of classes, methods, and variables across different packages. Non-access modifiers provide additional functionalities such as making classes or methods immutable, static, or abstract.

Core Content

Access Modifiers

Access modifiers determine the scope of visibility for classes, methods, and variables. Java provides four access levels:

  1. public
  2. protected
  3. default (no modifier)
  4. private

1. Public Modifier

The public modifier allows a class, method, or variable to be accessed from any other class in the same package or different packages.

PublicModifier.java
1public class PublicModifier {
2 public int x = 5;
3
4 public void display() {
5 System.out.println("x: " + x);
6 };
7}
8
9class AnotherClass {
10 public static void main(String[] args) {
11 PublicModifier obj = new PublicModifier();
12 obj.display(); // Output: x: 5
13 }
14}
Output
x: 5

Tip: Use the public modifier when you want to expose your class, method, or variable to all other classes.

2. Private Modifier

The private modifier restricts a class, method, or variable so that it can only be accessed within its own class.

PrivateModifier.java
1public class PrivateModifier {
2 private int x = 5;
3
4 private void display() {
5 System.out.println("x: " + x);
6 }
7
8 public static void main(String[] args) {
9 PrivateModifier obj = new PrivateModifier();
10 // obj.display(); // This will cause a compile-time error
11 System.out.println(obj.x); // This will also cause a compile-time error
12 }
13}

Tip: Use the private modifier to encapsulate your class members, ensuring they are not accessible from outside the class.

3. Protected Modifier

The protected modifier allows a class, method, or variable to be accessed within its own package and by subclasses (even if they are in different packages).

ProtectedModifier.java
1package com.example.package1;
2
3public class Parent {
4 protected int x = 5;
5
6 protected void display() {
7 System.out.println("x: " + x);
8 }
9}
10
11// In a different package
12package com.example.package2;
13
14import com.example.package1.Parent;
15
16public class Child extends Parent {
17 public static void main(String[] args) {
18 Child obj = new Child();
19 obj.display(); // Output: x: 5
20 System.out.println(obj.x); // Output: 5
21 }
22}

Tip: Use the protected modifier when you want to allow access within the same package and subclasses, but not from other packages.

4. Default Modifier (Package-Private)

When no access modifier is specified, a class, method, or variable has default visibility. It can only be accessed within its own package.

DefaultModifier.java
1package com.example.package1;
2
3class DefaultClass {
4 int x = 5;
5
6 void display() {
7 System.out.println("x: " + x);
8 }
9}
10
11// In a different package
12package com.example.package2;
13
14import com.example.package1.DefaultClass;
15
16public class AnotherClass {
17 public static void main(String[] args) {
18 DefaultClass obj = new DefaultClass();
19 // obj.display(); // This will cause a compile-time error
20 // System.out.println(obj.x); // This will also cause a compile-time error
21 }
22}

Tip: Use the default modifier when you want to restrict access to the same package only.

Non-Access Modifiers

Non-access modifiers provide additional functionalities to classes, methods, and variables. We'll cover three non-access modifiers: final, static, and abstract.

1. Final Modifier

The final modifier can be applied to classes, methods, and variables.

  • Final Class: A class declared as final cannot be subclassed.
  • Final Method: A method declared as final cannot be overridden by subclasses.
  • Final Variable: A variable declared as final must be initialized at the time of declaration and cannot be changed afterward.
FinalModifier.java
1public class FinalClass {
2 final int x = 5;
3
4 public final void display() {
5 System.out.println("x: " + x);
6 }
7
8 public static void main(String[] args) {
9 FinalClass obj = new FinalClass();
10 obj.display(); // Output: x: 5
11 }
12}
13
14// Attempting to subclass a final class will cause a compile-time error
15// class SubFinalClass extends FinalClass {} // This will not compile

Tip: Use the final modifier when you want to prevent further modification or inheritance.

2. Static Modifier

The static modifier can be applied to methods and variables.

  • Static Method: A method declared as static belongs to the class rather than any object of the class. It can be called without creating an instance of the class.
  • Static Variable: A variable declared as static is shared among all instances of the class. Changes to a static variable are reflected across all objects.
StaticModifier.java
1public class StaticModifier {
2 static int x = 5;
3
4 public static void display() {
5 System.out.println("x: " + x);
6 }
7
8 public static void main(String[] args) {
9 StaticModifier.display(); // Output: x: 5
10 System.out.println(StaticModifier.x); // Output: 5
11
12 StaticModifier obj = new StaticModifier();
13 obj.x = 10;
14 System.out.println(obj.x); // Output: 10
15 }
16}

Tip: Use the static modifier when you want to associate a method or variable with the class itself, rather than any specific instance.

3. Abstract Modifier

The abstract modifier can be applied to classes and methods.

  • Abstract Class: A class declared as abstract cannot be instantiated on its own. It must be subclassed.
  • Abstract Method: A method declared as abstract does not have an implementation in the abstract class. Subclasses must provide implementations for these methods.
AbstractModifier.java
1abstract class Animal {
2 abstract void makeSound();
3
4 void breathe() {
5 System.out.println("Breathing...");
6 }
7}
8
9class Dog extends Animal {
10 void makeSound() {
11 System.out.println("Bark");
12 }
13}
14
15public class Main {
16 public static void main(String[] args) {
17 // Animal obj = new Animal(); // This will cause a compile-time error
18 Dog dog = new Dog();
19 dog.makeSound(); // Output: Bark
20 dog.breathe(); // Output: Breathing...
21 }
22}

Tip: Use the abstract modifier when you want to define a class that cannot be instantiated on its own and requires subclasses to provide specific implementations.

Practical Example

Let's create a practical example that demonstrates the use of access and non-access modifiers in a real-world scenario. We'll create a simple banking application where we have an abstract class Account with different types of accounts like SavingsAccount and CheckingAccount.

BankingApp.java
1abstract class Account {
2 protected String accountNumber;
3 protected double balance;
4
5 public Account(String accountNumber, double initialBalance) {
6 this.accountNumber = accountNumber;
7 this.balance = initialBalance;
8 }
9
10 abstract void deposit(double amount);
11
12 abstract void withdraw(double amount);
13
14 public final void displayBalance() {
15 System.out.println("Account Number: " + accountNumber);
16 System.out.println("Balance: $" + balance);
17 }
18}
19
20class SavingsAccount extends Account {
21 private static int interestRate = 5;
22
23 public SavingsAccount(String accountNumber, double initialBalance) {
24 super(accountNumber, initialBalance);
25 }
26
27 @Override
28 void deposit(double amount) {
29 balance += amount;
30 }
31
32 @Override
33 void withdraw(double amount) {
34 if (amount <= balance) {
35 balance -= amount;
36 } else {
37 System.out.println("Insufficient funds");
38 }
39 }
40
41 public static int getInterestRate() {
42 return interestRate;
43 }
44}
45
46class CheckingAccount extends Account {
47 private double overdraftLimit;
48
49 public CheckingAccount(String accountNumber, double initialBalance, double overdraftLimit) {
50 super(accountNumber, initialBalance);
51 this.overdraftLimit = overdraftLimit;
52 }
53
54 @Override
55 void deposit(double amount) {
56 balance += amount;
57 }
58
59 @Override
60 void withdraw(double amount) {
61 if (amount <= balance + overdraftLimit) {
62 balance -= amount;
63 } else {
64 System.out.println("Exceeds overdraft limit");
65 }
66 }
67}
68
69public class Main {
70 public static void main(String[] args) {
71 SavingsAccount savings = new SavingsAccount("SA001", 1000);
72 CheckingAccount checking = new CheckingAccount("CA001", 500, 200);
73
74 savings.deposit(200);
75 savings.withdraw(300);
76 savings.displayBalance(); // Output: Account Number: SA001
77 // Balance: $900
78
79 checking.deposit(100);
80 checking.withdraw(800);
81 checking.displayBalance(); // Output: Account Number: CA001
82 // Balance: -$300
83 }
84}

Summary

ModifierDescription
Access Modifiers
publicAccessible from any class
protectedAccessible within the same package and subclasses
defaultAccessible within the same package only (no modifier)
privateAccessible only within its own class
Non-Access Modifiers
finalPrevents further modification or inheritance
staticAssociated with the class, not an instance
abstractDefines a class that cannot be instantiated and requires subclasses to provide implementations

What's Next?

In the next lesson, we'll dive into Java Encapsulation, which is a fundamental concept in object-oriented programming. Encapsulation helps in bundling the data (variables) with the methods that operate on the data, thereby protecting it from unauthorized access.

Stay tuned for more insights and practical examples to enhance your Java programming skills!


PreviousJava ConstructorsNext Java Encapsulation

Recommended Gear

Java ConstructorsJava Encapsulation