
Good afternoon, Habrovchane! I have been programming in java for quite some time, and often use nested classes, but recently I came across a static nested class and realized that I know almost nothing about it. Therefore, I decided to understand this, systematize my knowledge, and at the same time share this knowledge with you.
Nested class (InnerClass)
public class OuterClass { public class InnerClass{ } }
From it are visible:- all (even private) OuterClass properties and methods are normal and static.
- public and protected properties and methods of the parent OuterClass ordinary and static. That is, those that are visible in the OuterClass.
It can be seen:- according to the access modifier.
')
Can inherit:- ordinary classes.
- the same inner classes in the OuterClass and its ancestors.
Can be inherited:- the same inner class in the OuterClass and its heirs.
Can implement interfaceMay contain:- only common properties and methods (non-static).
An instance of this class is created like this: OuterClass outerClass = new OuterClass(); OuterClass.InnerClass innerClass = outerClass.new InnerClass();
Static nested class (StaticInnerClass)
public class OuterClass { public static class StaticInnerClass{ } }
From it (the class itself) are visible:- OuterClass static properties and methods (even private).
- static properties and methods of the parent OuterClass public and protected. That is, those that are visible in the OuterClass.
From its copy are visible:- all (even private) OuterClass properties and methods are normal and static.
- public and protected properties and methods of the parent OuterClass ordinary and static. That is, those that are visible in the OuterClass.
It can be seen:- according to the access modifier.
Can inherit:- ordinary classes.
- the same static inner classes in the OuterClass and its ancestors.
Can be inherited:- any class:
- nested
- not nested
- static
-
not static!Can implement interfaceMay contain:- static properties and methods.
- not static properties and methods.
An instance of this class is created like this: OuterClass.StaticInnerClass staticInnerClass = new OuterClass.StaticInnerClass();
Local class (LocalClass)
public class OuterClass { public void someMethod(){ class LocalClass{ } } }
From it are visible:- all (even private) OuterClass properties and methods are normal and static.
- public and protected properties and methods of the parent OuterClass ordinary and static. That is, those that are visible in the OuterClass.
It can be seen:- only in the method where it is defined.
Can inherit:- ordinary classes.
- inner classes in the OuterClass and its ancestors.
- the same local classes defined in the same method.
Can be inherited:- the same local class defined in the same method.
Can implement interfaceMay contain:- only common properties and methods (non-static).
Anonymous class (no name)
Local class with no name. Inherits some class, or implements some kind of interface.
public class OuterClass { public void someMethod(){ Callable callable = new Callable() { @Override public Object call() throws Exception { return null; } }; } }
From it are visible:- all (even private) OuterClass properties and methods are normal and static.
- public and protected properties and methods of the parent OuterClass ordinary and static. That is, those that are visible in the OuterClass.
It can be seen:- only in the method where it is defined.
Cannot be inheritedMay contain:- only common properties and methods (non-static).
That's all. Waiting for your comments: what are the inaccuracies and errors that I did not cover, etc.
I hope this article will be useful to many.