class Outer{
class Inner extends HasStatic{
static final x = 3; static int y = 4; //line1 // compile-time error, an inner class
}
static class NestedButNotInner{ // line2
static int z = 5; //line3
}
}
1.What is the difference between Inner class and Nested class.
There are for type of nested classes:
a. Top level nested classes or interfaces (always static)
b. Non static inner class (always non-static)
c. Local class (can be static or non-static)
d. Anonymous class (can be static or non-static)
The last three are collectively called "inner classes".
3. line2 is not inner class?
Line 2 is top level nested class because it is static but not inner by definition.
4. If I define class starting with static initializer inside another class that will not become inner class? Will it become netsted class?
Yes.
5. Why line3 will not give compile-time error?
Top level nested class can have static members. Non-static inner class can not.
Hope this helps.
Barkat