Back to Java
2026-04-075 min read

Java Modifiers

Learn Java Modifiers step by step with clear examples and exercises.

Title: Java Modifiers - Mastering Access Control and Variable Scope

Why This Matters

Java modifiers play a crucial role in defining access control, variable scope, and behavior of classes, methods, and variables. Understanding them is essential for writing clean, efficient, and secure code. They are vital when preparing for job interviews, coding challenges, or real-world programming tasks.

Prerequisites

Before diving into Java modifiers, you should have a solid understanding of:

  1. Basic Java syntax (variables, methods, classes)
  2. Control structures (if-else, loops, etc.)
  3. Understanding the concept of access control and variable scope in programming

Core Concept

Java provides several modifiers to alter the behavior of classes, interfaces, methods, constructors, variables, and local variables. In this lesson, we will focus on the following modifiers:

  1. public - allows unrestricted access from any part of the application
  2. private - restricts access within the same class only
  3. protected - allows access within the same package and subclasses in other packages
  4. default (no modifier) - accessible only within the same package
  5. static - applies to class variables and methods, which can be accessed without creating an object
  6. final - prevents modification of a variable or method
  7. abstract - used for abstract classes and methods that must be overridden in subclasses

Access Modifiers

Access modifiers control the visibility of classes, variables, and methods within your codebase.

Public (public)

A public access modifier grants unrestricted access to any part of the application:

public class MyClass {
public int myPublicVariable = 10;

public void myPublicMethod() {
System.out.println("Hello, World!");
}
}

Private (private)

A private access modifier restricts access to the same class only:

public class MyClass {
private int myPrivateVariable = 20;

public void printPrivate() {
System.out.println(myPrivateVariable);
}
}

Protected (protected)

A protected access modifier allows access within the same package and subclasses in other packages:

package com.example; // Package declaration

public class MyClass {
protected int myProtectedVariable = 30;
}

// In another package
public class SubClass extends MyClass {
public void printProtected() {
System.out.println(myProtectedVariable);
}
}

Default (no modifier)

A default access modifier is accessible only within the same package:

package com.example; // Package declaration

public class MyClass {
int myDefaultVariable = 40;
}

Static Modifier

The static modifier applies to class variables and methods, which can be accessed without creating an object:

public class MyClass {
public static int myStaticVariable = 50;

public static void myStaticMethod() {
System.out.println("Hello from a static method!");
}
}

// Accessing the static member without creating an object
System.out.println(MyClass.myStaticVariable);
MyClass.myStaticMethod();

Final Modifier

The final modifier prevents modification of a variable or method:

public class MyClass {
public final int MY_CONSTANT = 60; // Constant value cannot be changed

public void myFinalMethod() {
System.out.println("This method is final and cannot be overridden");
}
}

Abstract Modifier

The abstract modifier is used for abstract classes and methods that must be overridden in subclasses:

public abstract class MyAbstractClass {
public abstract void myAbstractMethod(); // Must be overridden in a concrete subclass
}

Worked Example

Let's create a simple example using access modifiers and static methods:

public class MyClass {
private int myPrivateVariable = 10;
protected int myProtectedVariable = 20;
int myDefaultVariable = 30;
public static int myStaticVariable = 40;
final int MY_CONSTANT = 50;

public void printVariables() {
System.out.println("myPrivateVariable: " + myPrivateVariable);
System.out.println("myProtectedVariable: " + myProtectedVariable);
System.out.println("myDefaultVariable: " + myDefaultVariable);
System.out.println("myStaticVariable: " + MyClass.myStaticVariable);
}

public static void printConstant() {
System.out.println("MY_CONSTANT: " + MY_CONSTANT);
}
}

Common Mistakes

  1. Forgetting to include the access modifier for a class, variable, or method.
  2. Using public and private interchangeably when they have different levels of accessibility.
  3. Not understanding the difference between protected and default access modifiers.
  4. Misusing the static modifier by declaring instance variables as static when they should be non-static.
  5. Declaring a method as both final and abstract, which is not allowed in Java.

Practice Questions

  1. Write a class with a private variable, a public method to get the value of that variable, and an instance of another class that uses this method.
  2. Explain the difference between public, private, and protected access modifiers in Java.
  3. Create a simple program using static methods and variables.
  4. Write an abstract class with an abstract method and a concrete subclass that overrides it.
  5. What is the purpose of the final modifier in Java? Provide an example of its usage.

FAQ

Q: Can I make a private method accessible to other classes within the same package?

A: No, private methods are only accessible within their own class. However, you can make them protected if you want subclasses in other packages to access them.

Q: What happens when a variable is declared as both static and final?

A: A static final variable is a constant value that is shared among all instances of the class and cannot be changed once set.

Q: Can I declare an instance method as final in Java?

A: Yes, you can declare an instance method as final, but it cannot be overridden by subclasses.

Q: Is it possible to make a variable accessible within the same package and private for other packages using a single modifier?

A: No, you need to use both default (no modifier) for access within the same package and private for other packages if you want to achieve this level of access control.

Q: What is the purpose of the abstract modifier in Java?

A: The abstract modifier is used to create an abstract class, which cannot be instantiated on its own. Abstract classes can have abstract methods that must be overridden by concrete subclasses.

Java Modifiers | Java | XQA Learn