Skip to main content

Am I missing something about nested static classes?

1 reply [Last post]
dwaroncon
Offline
Joined: 2009-01-23
Points: 0

In http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html, in the section titled "Static Nested Classes":
...
Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
...

If OuterClass.StaticNestedClass *is* static, then how can you create a OuterClass.StaticNestedClass object?

Or does this example need reworking?

Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
prunge
Offline
Joined: 2004-05-06
Points: 0

A static nested class means you don't need an [i]instance[/i] of the outer class to use the nested class. You can still create instances of the static nested classes.

This is opposed to a non-static inner class, which you must create through an instance, e.g.

[code]
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = outer.new InnerClass();
[/code]