Access Specifiers and Variable Scope in Java (ICSE Class 10)
Updated 17 October 2026 · Fact-checked
Access specifiers decide who can use a class member: private allows only the same class, protected allows the same class, the same package and sub classes, and public allows every class. Scope is the part of a program where a variable can be used: a local variable only inside its method or block, an argument variable inside its method, an instance variable inside the object, and a class (static) variable throughout the class.
Private data sits behind the class wall; only public methods like setAge() can reach it, and they can check the value first.
Illustration drawn for QuestionBuddy.
Understand Access Specifiers and Variable Scope Programs
Encapsulation in practice. Variables are made private so that other classes cannot change them directly; public methods (getters and setters) give controlled access, and a setter can check the value first. This is data hiding.
Visibility rules.
private: accessible only inside the class in which it is declared.protected: accessible in the class, in other classes of the same package, and in its sub classes.public: accessible from any class.
Using a private member from another class is a compile-time error: *bal has private access in Acct*.
Getter and setter.
class Person {
private int age = 10;
int getAge() { return age; }
void setAge(int a) { if (a > 0) age = a; }
}Direct use p.age = -5; from another class is blocked; p.setAge(-5) is accepted but ignored by the check.
Scope and lifetime of variables.
- *Local variable*: declared inside a method or block; usable from its declaration to the end of that block; it exists only while the block runs and has no default value (must be assigned before use).
- *Argument (parameter) variable*: local to the method; receives the value passed.
- *Instance variable*: declared in the class without static; one copy per object; lives as long as the object.
- *Class variable*: declared with static; one copy shared by all objects; lives as long as the class.
Shadowing. If a local or argument variable has the same name as an instance variable, the plain name means the local one; this.name means the instance variable. Two variables of the same name cannot be declared in the same scope or in nested scopes of one method (int i and then for (int i...) is an error), although two separate loops may each declare i.
Block scope. A variable declared in the header or body of a for loop cannot be used after the loop ends.
Access Specifiers and Variable Scope Programs: key facts
- Visibility rules
private same class only protected same class, same package, sub classes public all classes - Order from narrowest to widest: private, protected, public.
- Four kinds of variables
Local inside a method or block Argument in the method header Instance in the class, one per object Class static, one per class - Only local variables have no default value.
- Controlled access
- Make variables private
- Provide public getters
- Provide public setters with checks
- This is encapsulation (data hiding).
- Name clash
- Plain name: nearest variable (local or argument) | this.name: the instance variable
- Same name twice in one scope is an error.
How to answer Access Specifiers and Variable Scope Programs questions
Use this routine for access and scope questions.
- 1Find which class each line of code is written in.
- 2For each member used, check its specifier against the class that uses it.
- 3For variables, mark the block in which each is declared and check each use falls inside that block.
- 4When names clash, use the nearest declaration for the plain name and the instance variable for this.name.
- 5If a rule is broken, name the compile-time error and give the fix (getter, setter or a wider specifier).
Access Specifiers and Variable Scope Programs in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Private balance, public passbook
Suppose a bank lets customers see their balance through a passbook and deposit through a counter, but nobody can open the vault and change the ledger directly.
Map the vault, the passbook and the counter to private, getter and setter.
Try it first, then show the solutionSolution
- The ledger in the vault is the private variable.
- The passbook shows it (getter) and the counter changes it after checking the request (setter).
Answer: Vault: private variable. Passbook: getter. Counter: setter.
Memory hook: Hide the data, publish the methods.
Tricks to remember Access Specifiers and Variable Scope Programs
Short memory aids. Say each one aloud once, then use it on a question the same day.
Private < Protected < Public
Each step to the right lets more classes in: only me, my family and neighbours, everyone.
Use it when: Visibility questions.
Brace is the boundary
A local variable lives only between the braces where it is declared.
Use it when: Scope errors.
this points at my own
this.x is the object's variable; plain x is the closest x.
Use it when: Same-name variables.
Access Specifiers and Variable Scope Programs: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Visibility rules · 3 marks · Short answerBoard level
State where a private, a protected and a public member can be accessed.
Try it first, then show the solutionSolution
- private: only inside the class in which it is declared. (1 mark)
- protected: inside the class, in classes of the same package and in sub classes. (1 mark)
- public: from any class. (1 mark)
Answer: private: same class; protected: class, package and sub classes; public: everywhere.
Example 2: Access error and the fix · 3 marks · Short answerHarder board level
The code below does not compile. Find the line and fix it.
class Acct {
private int bal = 100;
public int getBal() { return bal; }
}
public class Test {
public static void main(String[] args) {
Acct a = new Acct();
System.out.println(a.getBal());
System.out.println(a.bal);
}
}Try it first, then show the solutionSolution
- The line System.out.println(a.bal); uses a private variable from another class: error "bal has private access in Acct". (1 mark)
- Fix: use the getter, System.out.println(a.getBal()); (1 mark)
- After the fix the program prints 100 twice. (1 mark)
Answer: a.bal is the error; use a.getBal(). Output after the fix: 100 and 100.
Example 3: Argument, local and instance variables · 2 marks · Short answerHarder board level
Write the output:
class Sc {
int x = 10;
void show(int x) {
int z = x * 2;
System.out.println(x + " " + z + " " + this.x);
}
}
// in main
new Sc().show(4);Try it first, then show the solutionSolution
- The argument x = 4 hides the instance variable x inside show(); z = 4 * 2 = 8. (1 mark)
- this.x is the instance variable 10, so the output is 4 8 10. (1 mark)
Answer: 4 8 10
Example 4: Case study: kinds of variables in a Shop class · 4 marks · Case-basedHOTS / competency
Study the code:
class Shop {
static int shops = 0;
int stock;
void sell(int qty) {
int left = stock - qty;
stock = left;
}
}
public class Test {
public static void main(String[] args) {
Shop s = new Shop();
s.stock = 20;
s.sell(5);
System.out.println(s.stock + " " + Shop.shops);
}
}(i) Classify shops, stock, qty and left. (2)
(ii) State the output. (1)
(iii) Where can left be used? (1)
Try it first, then show the solutionSolution
- (i) shops: class (static) variable; stock: instance variable; qty: argument variable; left: local variable.
- (ii) sell(5): left = 20 - 5 = 15 and stock = 15. shops was never changed, so it is 0. Output 15 0.
- (iii) left exists only inside sell(); it cannot be used in main or anywhere else.
Answer: (i) class, instance, argument, local (ii) 15 0 (iii) only inside sell()
Example 5: Block scope of a loop variable · 3 marks · Short answerHarder board level
Write the output, and say what happens if System.out.println(i); is added at the end of main:
int a = 5;
for (int i = 0; i < 3; i++) {
int t = i * a;
a = a + 1;
}
System.out.println(a);Try it first, then show the solutionSolution
- i = 0: t = 0, a = 6. i = 1: t = 6, a = 7. i = 2: t = 14, a = 8. (1 mark)
- The output is 8. (1 mark)
- i and t are declared inside the loop, so they cannot be used after it: println(i) gives a compile-time error (cannot find symbol). (1 mark)
Answer: 8; println(i) after the loop is a compile-time error
Where students lose marks in Access Specifiers and Variable Scope Programs
The slips examiners see most often, and what to write instead.
Mistake: Using a loop variable after the loop
A variable declared in the for header lives only within the loop.
Wrong working
for (int i = 0; i < 3; i++) { } System.out.println(i);
Right working
Declare int i before the loop if it is needed afterwards.
Do this: Declare outside the loop.
Usually costs: 1 mark
Mistake: Calling protected or private members "available everywhere"
Only public members are available to every class.
Wrong working
protected: accessible everywhere.
Right working
protected: same class, same package and sub classes.
Do this: Learn the three rules in order.
Usually costs: 1 mark
Mistake: Forgetting this. when a parameter has the same name
The plain name is the parameter, so the instance variable is not changed.
Wrong working
void set(int x) { x = x; }
Right working
void set(int x) { this.x = x; }
Do this: Write this.x on the left.
Usually costs: 1 mark
Exam tips
- Quote the visibility rules as three short lines; this is a favourite 3-mark answer.
- Name the four kinds of variables and give one line on scope for each.
- Explain data hiding: private variable plus public getter and setter.
- In error questions, state the exact message idea, such as "has private access".
Practice questions on Access Specifiers and Variable Scope Programs
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
Which access specifier allows access to a member only inside its own class?
(a) public (b) protected (c) private (d) staticHint
Most restricted.
Answer
(c) private
Q2 · 1 mark · Very short answerBoard level
What name is given to a variable declared inside a method?
Hint
Its scope is that method only.
Answer
A local variable.
Q3 · 2 marks · Short answerHarder board level
Write the output:
class T { int n = 3; void f() { int n = 7; System.out.println(n + " " + this.n); } } // in main new T().f();Hint
Plain n is the nearest declaration.
Answer
7 3
Q4 · 3 marks · Short answerHarder board level
Given class Person with private int age = 10; and the methods getAge() and setAge(int a) { if (a > 0) age = a; }, write the output of:
Person p = new Person();
p.setAge(-5);
System.out.println(p.getAge());
p.setAge(12);
System.out.println(p.getAge());
Also state why age is made private.Hint
The setter checks before assigning.
Answer
10
12. age is private so that other classes cannot give it an invalid value directly; the setter checks that the value is positive (data hiding).Q5 · 1 mark · Assertion-reasonHOTS / competency
Assertion (A): A private variable of a class cannot be accessed directly from another class.
Reason (R): The private specifier limits access to the class in which the member is declared.
(a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.Hint
Does R define private?
Answer
(a) Both are true and R explains A.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.Which specifier gives access to every class?Show answer
public
2.What is a getter?Show answer
A public method that returns the value of a private variable.
3.What is the scope of a local variable?Show answer
The method or block in which it is declared.
4.What does this.x refer to?Show answer
The instance variable x of the current object.
5.Which variable has one copy for the whole class?Show answer
A class (static) variable.
6.What must be done to a local variable before it is read?Show answer
It must be assigned a value; the compiler gives it no default.
Access Specifiers and Variable Scope Programs: frequently asked questions
Why make variables private?
To hide the data so that it can be changed only through methods that can check the values.
What is the difference between private and protected?
private allows only the same class; protected also allows classes of the same package and sub classes.
What happens if I do not write any specifier?
The member gets the default (package) access, which is outside the three specifiers named in the Class X syllabus.