Wednesday 22 January 2014

Static Nested Classes



Static member class, also called static nested classes- They are declared static. Like other things in static scope (i.e. static methods), they do not have an enclosing instance, and cannot access instance variables and methods of the enclosing class. They are almost identical to non-nested classes except for scope details (they can refer to static variables and methods of the enclosing class without qualifying the name; other classes that are not one of its enclosing classes have to qualify its name with its enclosing class's name). Nested interfaces are implicitly static.

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.

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

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

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

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass {
    ...
    class InnerClass {
        ...
    }
}

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();
There are two special kinds of inner classes: local classes and anonymous classes.
Nested static class is another class which is declared inside a class as member and made static. Nested static class is also declared as member of outer class and can be make private, public or protected like any other member. One of the main benefit of nested static class over inner class is that instance of nested static class is not attached to any enclosing instance of Outer class. You also don't need any instance of Outer class to create instance of nested static class in Java. This makes nested static class very convenient to use and access.

Here is an example of nested static class in Java. It look exactly similar to member inner classes but has quite a few significant difference with them, e.g. you can access them inside main method because they are static. In order to create instance of nested static class, you don’t need instance of enclosing class. You can refer them with class name and you can also import them using static import feature of Java 5.

public class NestedStaticExample {

    public static void main(String args[]){
 
        StaticNested nested = new StaticNested();
        nested.name();
    }
 
    //static nested class in java
    private static class StaticNested{
        public void name(){
            System.out.println("static nested class example in java");
        }
    }
}



No comments:

Post a Comment