static Member Class Is Not an Inner Class. It is a top-level class. It is also called a nested top-level class.
A member class defined within the body of another class may be declared static.
You do not need an instance of its enclosing class to create its object.
The following code declares a top-level class A and a static member class B:
class A { // Static member class public static class B { // Body for class B goes here } }
An instance of class A and an instance of class B can exist independently.
A static member class can be declared public, protected, package-level, or private to restrict its accessibility outside its enclosing class.
A static member class can access the static members of its enclosing class including the private static members.
Top-level classes having static member classes provide an additional layer of namespaces.
To create an object of class B, you write
A.B bReference = new A.B();
The following statement appears inside class A code
B bReference2 = new B();
An Example of Declaring Static Member Classes
class Computer { // Static member class - Monitor public static class Monitor { private int size; public Monitor(int size) { this.size = size; }/*from ww w . j ava2 s. c o m*/ public String toString() { return "Monitor - Size:" + this.size + " inch"; } } // Static member class - Keyboard public static class Keyboard { private int keys; public Keyboard(int keys) { this.keys = keys; } public String toString() { return "Keyboard - Keys:" + this.keys; } } } public class Main { public static void main(String[] args) { // Create two monitors Computer.Monitor m = new Computer.Monitor(17); Computer.Monitor m1 = new Computer.Monitor(19); // Create two Keyboards Computer.Keyboard k1 = new Computer.Keyboard(122); Computer.Keyboard k2 = new Computer.Keyboard(142); System.out.println(m); System.out.println(m1); System.out.println(k1); System.out.println(k2); } }