1.Define the constructor as private
2.Expose a method that can be used to create the object - getObject()
3. Synchronize the exposed method getObject method
4. Define a private Static - which would store a single instance.
OuterClass, a nested class can be declared private, public, protected, or package privateOuterClass.InnerClass innerObject = outerObject.new InnerClass(); An instance ofInnerClasscan exist only within an instance ofOuterClassand has direct access to the methods and fields of its enclosing instance. Also, because an inner class is associated with an instance, it cannot define any static members itself. Static Nested Class : As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
| Type | Scope | Inner |
|---|---|---|
| static nested class | member | no |
| inner [non-static] class | member | yes |
| local class | local | yes |
| anonymous class | only the point where it is defined | yes |