Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java nested interfaces and inner classes

Why can't a java nested Interface be non-static ? And why can't an inner class contain static non final members ?

I came across the questions while going through Gosling and haven't been able to figure out the answer yet.

like image 406
S..K Avatar asked Apr 08 '11 09:04

S..K


People also ask

What is difference between nested and inner class in Java?

Non-static nested classes are called inner classes. Nested classes that are declared static are called static nested classes. 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, even if they are declared private.

What is nested class and interface in Java?

Java inner class or nested class is a class that is declared inside the class or interface. We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable. Additionally, it can access all the members of the outer class, including private data members and methods.

Can a class have nested interface in Java?

Yes, you can define an interface inside a class and it is known as a nested interface.

What are nested interfaces in Java?

An interface, i.e., declared within another interface or class, is known as a nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface must be referred to by the outer interface or class. It can't be accessed directly.


1 Answers

If an nested class is non-static (i.e. an inner class), this means that each instance of it is bound to an instance of the outer class. As an interface has no instances of its own, it seems to not be useful for the implementing classes to be bound to an outer object, so having it static by default seems reasonable.

like image 89
Paŭlo Ebermann Avatar answered Sep 22 '22 03:09

Paŭlo Ebermann