2

I just want the differences on the basis of how members are accessible from enclosing class or from inner class of each other.

In relation to static, final, effectively final members and why?

0

3 Answers 3

2

A static nested class is effectively the same as using a separate class file except you put it inside another one.

If it is not static however it is called an inner class. The inner class has access to all members of its enclosing class. In case of a static nested class they can exist without the other being instantiated but an inner class can only exist within the instance of its enclosing class.

You should pay attention to the naming of the concepts. So in short:

  • static nested class: almost same as a normal class but enclosed in an other one. Has access to public membrs
  • inner class: Can only exist withing the instance of its enclosing class. Has access to all members.
  • local class: class declared in a block. It is like an inner class (has access to all members) but it also has access to local scope.

The official documentation explains this in detail so I suggest you should read through it. Information about local and anonymous inner classes can be found there as well.

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

1 Comment

Nesting a static class isn't 'effectively the same as using a separate class file except you put it inside another one', because of all the extra variables that are in scope. A static nested class doesn't just 'have access to public members' of the enclosing class. It has access to all members, but it can only directly use static members, because of the absence of an intrinsic handle to an outer object. public has nothing to do with it.
1

private members of the enclosing class are visible from the nested class, and likewise, private members of the nested class are visible to the outer class.

If the nested class is static, then only static members of the outer class may be seen, including private static.

You need an instance of the outer class in order to instantiate the nested class, unless the nested class is static. This makes sense - for non-static nested classes, you can only access the class definition of the nested class from an instance of the outer class, just as if it were a member variable.

As well as nested classes, there are also anonymous inner classes. These cannot be static, but clearly their scope is limited to the method in which they are defined.

1 Comment

'inner' and 'static' are mutually exclusive. An inner class can't also be static. You need to talk about 'nested' rather than 'inner' in most of this.
-1

I just want the differences on the basis of how members are accessible from enclosing class or from inner class of each other.

There are no differences. Each can see all the other's members.

You didn't ask, but there is a difference if the enclosed class is static. A static nested class can only access the static members of the enclosing class. [It can 'see' the non-static members in terms of access control, but it has no intrinsic handle to an outer object so it has no way of accessing them.]

In relation to static, final, effectively final members and why?

This isn't even a sentence, let alone a question.

2 Comments

In relation to static, final, effectively final members and why? I asked this because official docs says "local variables referenced from an inner class must be final or effectively final" but why it does not tell.
Then you needed to make your question clearer. The reason is that the object created will probably outlive the method it is called from, and therefore outlive the local variable it is accessing. So, it makes a copy in the new object. So there are now two of them. So Java forces you to make the local variable final so you don't confuse yourself by updating one and not seeing the update in the other.

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.