static abstract inner classes

Sorry to sound stupid, but why would a class be static? I've encountered an example of anabstract static inner class and I'm not sure why it's static. Static methods I understand -- they are methods of the class and not the instance. So then, can someone explain to me astatic class?

Thanks,

Erik

[334 byte] By [erikprice] at [2007-9-27 22:22:21]
# 1

You have encountered a static nested class (not a static inner class - an inner class is a nested class that is not static).

A static nested class is a nested class that does not require an instance of its enclosing class to exist in order that it can be instantiated. - It is mainly used for code organisation, and can be treated just like a top-level class.

In practice, most nested classes (except perhaps event handlers), tend to be static.

For an example of a static nested class from the core APIs, have a look at HasMap.Entry

mattbunch at 2007-7-7 12:46:12 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
Excellent explanation. I understand now.Is "System.out" another example of a static nested class?
erikprice at 2007-7-7 12:46:13 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3

> Excellent explanation. I understand now.

>

> Is "System.out" another example of a static nested

> class?

>

>

No, out is just a static member variable of the System class. You can't declare System.out objects. You can declare HashMap.Entry objects though.

Jetset_Willy at 2007-7-7 12:46:13 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4
FYI, some more info on this subject can be found at Sun's java tutorial: http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
hungyee at 2007-7-7 12:46:13 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5
Normal inner classes can access methods and data of the object that they were created in. Static classes cannot do that. The flip side of this is that normal inner classes cannot be instantiated outside of object methods in the class, whereas static inner classes can be created anywhere
tjacobs01 at 2007-7-7 12:46:13 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6

> inner classes cannot be instantiated outside of object

> methods in the class, whereas static inner classes can

> be created anywhere

This is incorrect.

Inner classes _can_ be instantiated from anywhere that they are visible - if you declare an inner class public (inside a public class), and provide a public constructor, then it can be instantiated from anywhere.

mattbunch at 2007-7-7 12:46:13 > top of Java-index,Archived Forums,New To Java Technology Archive...