difference

Can anybody explain me difference between inner class and static class..

and where to use this

[107 byte] By [bala123456789a] at [2007-11-27 10:55:02]
# 1

Non-static inner classes are inner classes that only exists within instances of the outer type.

Static inner classes can exist without instances of the outer type.

(One of them is called "nested" and I can never memorize which one.)

CeciNEstPasUnProgrammeura at 2007-7-29 11:53:51 > top of Java-index,Java Essentials,Java Programming...
# 2

To instantiate an inner class, you must have a reference to an instance of the outer class.

From code within the enclosing class, you can instantiate the inner class using only the name of the inner class, as follows:

MyInner mi = new MyInner();

For the inner class to be used, you must instantiate it, and that instantiation must happen within the same method, but after the class definition code.

Static nested classes are inner classes marked with the static modifier.

Technically, a static nested class is not an inner class, but instead is considered a top-level nested class.

Because the nested class is static, it does not share any special relationship with an instance of the outer class. In fact, you dont need an instance of the outer class to instantiate a static nested class.

Instantiating a static nested class requires using both the outer and nested class names as follows:

BigOuter.Nested n = new BigOuter.Nested();

A static nested class cannot access nonstatic members of the outer class, since it does not have an implicit reference to any outer instance (in other words, the nested class instance does not get an outer this reference).

Just as a static method does not have access to the instance variables and methods of the class, a static nested class does not have access to the instance variables and methods of the outer class. Look for static nested classes with code that behaves like a nonstatic (regular inner) class.

passion_for_javaa at 2007-7-29 11:53:51 > top of Java-index,Java Essentials,Java Programming...