GauravS
GauravS

Reputation: 33

Accessing non-static nested class

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?

Upvotes: 1

Views: 77

Answers (1)

Tim B
Tim B

Reputation: 41168

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

Upvotes: 3

Related Questions