← Back to Core Java Course | Chapter 7: OOP Core | Lesson 5 of 11

Java Access Modifiers

Access modifiers decide who is allowed to see or use part of a class, like locks and keys on doors. private is for family only, and public is for everyone.
Syntax
java
public dataType field;     // everywhere
protected dataType field;  // package + subclasses
dataType field;            // package only
private dataType field;    // same class only

What are Access Modifiers?

Access modifiers control which parts of your codebase are allowed to see and use a given class, field, or method, forming the basis of encapsulation — they're the mechanism that actually enforces the 'hide internal details' principle of OOP.

Example: What are Access Modifiers?

java
// Define the class Account
class Account {
	private double balance = 100;
	public double getBalance() {
		// Send balance back to the caller
		return balance;
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Account object and store it in acc
		Account acc = new Account();
		// Print a line to the console
		System.out.println(acc.getBalance());
	}
}

The Private Modifier

private is the most restrictive level: a private member is visible only inside the class where it's declared, not even to subclasses. This is the default choice for internal implementation details you never want other code touching directly.

Example: The Private Modifier

java
class Account {
	private double balance = 100;
}
public class Main {
	public static void main(String[] args) {
		Account acc = new Account();
		// acc.balance would not compile: private to Account
		System.out.println("Balance is private");
	}
}

Default Access

Leaving off a modifier entirely gives a member default (package-private) access, meaning any class in the same package can use it, but classes in other packages cannot — even subclasses outside the package are locked out unless they also use protected or public.

Example: Default Access

java
class Account {
	double balance = 100; // no modifier: package-private
}
public class Main {
	public static void main(String[] args) {
		Account acc = new Account();
		System.out.println(acc.balance); // visible: same package
	}
}

The Protected Modifier

protected opens visibility to the declaring class's own package plus any subclass anywhere, even in a different package — it's the modifier of choice for members a subclass legitimately needs to inherit and use, without exposing them to unrelated code.

Example: The Protected Modifier

java
class Vehicle {
	protected String type = "Generic";
}
class Car extends Vehicle {
	void printType() {
		System.out.println(type); // subclass can access protected member
	}
}
public class Main {
	public static void main(String[] args) {
		new Car().printType();
	}
}

Visibility Control

Choosing the tightest access level that still lets your code work is a core defensive-programming habit: it keeps your class's internal state safe from accidental external modification while still exposing exactly the operations callers actually need.

Example: Visibility Control

java
class Account {
	private double balance = 100;
	public double getBalance() {
		return balance; // tightest access + controlled exposure
	}
}
public class Main {
	public static void main(String[] args) {
		System.out.println(new Account().getBalance());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Making a field private and accessing it from another class, which is a compile error.
  2. Forgetting that no modifier means package-private access.
  3. Making everything public, which breaks encapsulation.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.