-1

Since we can not use the static modifier with a local class defined inside a method, and since Nonstatic nested classes are Inner classes, we could probably say that a method local class is a type of an Inner class.

But on the other hand, we say that instances of Inner classes CAN NOT exist without a Live instance of the enclosing class. But an instance of a method-local class defined in a static method CAN exist without a live instance of the enclosing class, right?

So what do I conclude from this? The first logic tells me that local classes are a type of Inner class, and the second piece of reasoning tells me that Local class is not an Inner class.

0

1 Answer 1

2

we say that instances of Inner classes CAN NOT exist without a Live instance of the enclosing class

No. From the JLS (emphasis mine):

An inner class C is a direct inner class of a class or interface O if O is the immediately enclosing type declaration of C and the declaration of C does not occur in a static context.

[...]

An instance i of a direct inner class C of a class or interface O is associated with an instance of O, known as the immediately enclosing instance of i.

This tells us that one may have an inner class that does not have an enclosing instance. In particular, this occurs in situations where it's defined in a static context.

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

2 Comments

So in other words, an inner class instance can not exist without a live instance of an enclosing class, except when the inner class is occurring in a static context (e.g. in a static method) , right?
A question: I know that if there is an inner class named C defined directly inside a top-level enclosing class named O (by directly , I mean at the level of other member declarations), then C is an immediate inner class of O. BUT say a local class A is defined in a method named M in a class named B, is A an immediate inner class of B? I am a bit confused by the use of the word 'immediate' there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.