Java Reference
In-Depth Information
Static Nested Classes
To make objects of a nested class type independent of objects of the enclosing class type, you can declare
the nested class as static :
public class Outside {
public static class Skinside {
// Details of Skinside
}
// Nested class
public class Inside {
// Details of Inside class...
}
// More members of Outside class...
}
Now with Skinside inside Outside declared as static , you can declare objects of this nested class type
independent from any objects of type Outside , and regardless of whether you have created any Outside
objects or not. For example:
Outside.Skinside example = new Outside.Skinside();
This is significantly different from what you needed to do for a non-static nested class. Now you must
use the nested class name qualified by the enclosing class name as the type for creating the object. Thus, the
name of a static nested class exists within the context of the outer class and therefore the nested class name
is qualified by the enclosing class name. Note that a static nested class can have static members, whereas a
non-static nested class cannot. A class containing both a static and a non-static nested class is illustrated in
Figure 5-12 .
FIGURE 5-12
 
 
Search WWH ::




Custom Search