Bird
0
0

Which of the following is the correct way to declare a private variable in a Java class?

easy📝 Syntax Q12 of 15
Java - Encapsulation

Which of the following is the correct way to declare a private variable in a Java class?

class Person {
? String name;
}
Aprivate
Bpublic
Cprotected
Dstatic
Step-by-Step Solution
Solution:
  1. Step 1: Recall Java access modifiers

    Private variables are declared with the keyword private to hide them inside the class.
  2. Step 2: Check the options

    Only private hides the variable from outside access, others allow wider access.
  3. Final Answer:

    private -> Option A
  4. Quick Check:

    Private keyword hides variables [OK]
Quick Trick: Use 'private' to hide variables inside class [OK]
Common Mistakes:
  • Using public instead of private for encapsulation
  • Confusing protected with private
  • Using static which controls memory, not access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes