Static Nested Classes
Static nested classes are declared in Java like this:
public class Outer {
public static class Nested {
}
}
In order to create an instance of theNested
class you must reference it by prefixing it with theOuter
class name, like this:
Outer.Nested instance = new Outer.Nested();
In Java a static nested class is essentially a normal class that has just been nested inside another class. Being static, a static nested class can only access instance variables of the enclosing class via a reference to an instance of the enclosing class.
Non-static Nested Classes (Inner Classes)
Non-static nested classes in Java are also calledinner classes. Inner classes are associated with an instance of the enclosing class. Thus, you must first create an instance of the enclosing class to create an instance of an inner class. Here is an example inner class definition:
public class Outer {
public class Inner {
}
}
Here is how you create an instance of theInner
class:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();