1

This may be silly to ask, but looking at following code raises a question.

public class Outer { public class Inner { public static final int variable = 100; } public static void main(String[] args) { int test = Outer.Inner.variable; // Inner Non-Static accessed // with Class reference? } } 

How can the non-static nested class be accessed with a class reference?

3
  • 2
    The field variable is static, so it can be accessed. That's basically the same concept as with a top-level class. Commented May 14, 2014 at 14:30
  • A non-static data member can not be accessed statically. Your example; however, is of a static data member which is accessed statically. Commented May 14, 2014 at 14:33
  • My query is how can the Non Static nested class 'Inner' be accessed by outer class reference 'Outer' ('Outer.Inner'??), whereas Inner is an Instance member? Commented May 15, 2014 at 12:37

1 Answer 1

3

The variable is static, and that is what matters. Since the variable is static you can always access it with the class reference.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, my query is how can the Non Static nested class 'Inner' be accessed by outer class reference 'Outer' ('Outer.Inner'??), whereas Inner is an Instance member?
Because you are access the class, not an instance of the class. The inner class exists as a class whether you create instances of it or not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.