The private access modifier keeps parts of your code hidden and safe inside a class. It helps control what other parts of the program can see or change.
Private access modifier in Java
private dataType variableName; private returnType methodName(parameters) { // method body }
Private members can only be used inside the same class.
Other classes cannot access private variables or methods directly.
age inside a class.private int age;
private void showMessage() { System.out.println("Hello!"); }
name and method setName are private, so only the Person class can use them.public class Person { private String name; private void setName(String newName) { name = newName; } }
This program shows a Car class with a private variable model and a private method displayModel. The private method is called inside a public method show. The main method creates a Car object and calls show to print the model.
public class Car { private String model; public Car(String model) { this.model = model; } private void displayModel() { System.out.println("Car model: " + model); } public void show() { displayModel(); } public static void main(String[] args) { Car myCar = new Car("Toyota"); myCar.show(); // myCar.displayModel(); // This line would cause an error if uncommented } }
Trying to access private members from outside the class will cause a compile error.
Use public methods to safely access or change private variables (this is called encapsulation).
Private access helps keep your code clean and less error-prone.
Private members are only visible inside their own class.
They help protect data and hide details from other parts of the program.
Use public methods to interact with private data safely.
Practice
private access modifier do in Java?Solution
Step 1: Understand the meaning of
Theprivateprivatemodifier restricts access to the member so that only code inside the same class can use it.Step 2: Compare with other access levels
Other modifiers likepublicorprotectedallow wider access, butprivateis the most restrictive.Final Answer:
It restricts access to the member only within its own class. -> Option AQuick Check:
Private means only inside class [OK]
- Confusing private with protected or public
- Thinking private allows subclass access
- Assuming private allows package access
count in a Java class?Solution
Step 1: Recall Java variable declaration syntax
In Java, the access modifier comes first, then the type, then the variable name.Step 2: Check each option
private int count; follows the correct order:private int count;. Others have wrong order or syntax.Final Answer:
private int count; -> Option CQuick Check:
Access modifier + type + name [OK]
- Placing 'private' after the type or variable name
- Mixing order of keywords
- Missing semicolon
class Test {
private int number = 5;
public void printNumber() {
System.out.println(number);
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.printNumber();
}
}Solution
Step 1: Understand private member access inside class
The variablenumberis private but accessed inside its own class methodprintNumber(), which is allowed.Step 2: Check main method output
The main method creates an object and callsprintNumber(), which prints the value 5.Final Answer:
5 -> Option DQuick Check:
Private accessible inside class method [OK]
- Thinking private prevents access even inside class
- Expecting compilation error due to private
- Confusing default value with actual initialization
class Person {
private String name = "Alice";
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
System.out.println(p.name);
}
}Solution
Step 1: Check access to private field from outside class
The fieldnameis private inPersonclass, so it cannot be accessed directly fromMainclass.Step 2: Understand Java access rules
Accessingp.namefromMaincauses a compilation error due to private access.Final Answer:
Cannot access private fieldnamefrom outside the class. -> Option BQuick Check:
Private fields block outside access [OK]
- Assuming private fields are accessible everywhere
- Thinking missing constructor causes error here
- Ignoring access modifiers
Solution
Step 1: Protect sensitive data with private fields
Passwords should be private to prevent direct access and protect security.Step 2: Provide controlled access via public method
A public method can check if a given password matches without revealing the actual password.Final Answer:
Make the password field private and provide a public method to check the password. -> Option AQuick Check:
Private data + public check method = secure design [OK]
- Making password public exposes sensitive data
- Returning password directly breaks security
- Using protected exposes data to subclasses unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
