Java Programming - Inner Classes - Discussion

Discussion :: Inner Classes - General Questions (Q.No.4)

4. 

Which statement is true about a static nested class?

[A]. You must have a reference to an instance of the enclosing class in order to instantiate it.
[B]. It does not have access to nonstatic members of the enclosing class.
[C]. It's variables and methods must be static.
[D]. It must extend the enclosing class.

Answer: Option B

Explanation:

Option B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class).

Option A is incorrect because static nested classes do not need (and can't use) a reference to an instance of the enclosing class.

Option C is incorrect because static nested classes can declare and define nonstatic members.

Option D is wrong because it just is. There's no rule that says an inner or nested class has to extend anything.


Ravi said: (Nov 9, 2015)  
Static class can access to non static member of enclosing class through an object. So the option B is wrong.

Purnima said: (Jan 25, 2018)  
Even C option is also correct, why because its variables and methods must be static.

Yogesh Killedar said: (Nov 21, 2019)  
Option B is Wrong because a nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.