Static Inner Class

     

Inner classes can be static, meaning that they have access only to the static member variables of the outer class. The reason is that they don't share much of a relationship with the outer class ”they're tied to the name of the outer class, but not an instance of the outer class. A static nested class can be instantiated without an instance of the outer class. You can access it just like any static member variable. These are also known as top-level nested classes because they really boil down to a way to control namespace.

For example, in C#, you can define arbitrary namespaces that have no correspondence to actual directory structure (not so in Java), and you can nest namespaces within a class or a bit of code almost willy-nilly (again, not so in Java). So, the static nested class lets you get a similar level of namespace control. To which I say, "Big deal."

So, the static nested class has a life all its own, without an outer object enclosing it, and is finally free to pursue its amateur ham radio license. I have to say, though, that these things ain't used much.

StaticNestedClass.java

 

 package net.javagarage.inner; /**<p>  * Demos static inner class usage.  **/ public class StaticOuter {     //an instance of inner object cannot     //directly reference this, nor can its methods     int outerVar;     //an instance of inner object cannot     //directly reference static members     //BUT the members     //(methods and fields) defined inside the     //StaticInner class CAN     public static int outerStaticVar;     //Here is the Static inner class, defined     //like a member of the StaticOuter class     public static class StaticInner {    int innerVar;     //The StaticInner class methods can     //access only the static members of     //the enclosing class     public int getOuterStaticVar() {         return outerStaticVar;     } }//end StaticInner public static void main(String[] args) {     //this statement creates a new instance     //of the StaticInner class ONLY     StaticOuter.StaticInner inner =                       new StaticOuter.StaticInner();     inner.innerVar = 5;     //Instantiating the outer class does     //NOT give us any instance of the     //StaticInner class     StaticOuter outer = new StaticOuter();     //yeah, yeah outer can reference its     //OWN members per usual...     outer.outerVar = 10;     StaticOuter.outerStaticVar = 20;     //Wrong! I can only access this guy     //from WITHIN the methods of     //the StaticInner class     //inner.outerStaticVar = 20; //wrong!!     //prints 20.     System.out.println(inner.getOuterStaticVar());   } } 

The methods of a static inner class can access only the static members of the outer class.

So that ends our adventure through the fascinating land of inner classes. They are often useful when applied in conventional ways, such as implementing an actionPerformed method for a JButton . Just be careful not to overuse them and possibly introduce unnecessary complexity and confusion in your code. As my friend Erin from Indiana used to say, "eschew obfuscation."



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net