Static vs non Static class in Java
In Java you can make a class either static or non static. Now what is difference between making a class static vs non static? well there is lot of difference between them. First of all there are two kinds of class in Java, one is called top level class and other is called nested class. As name suggested top level class is a class which is declared in .java file and not enclosed under any other class. On other hand nested class is declared inside another class. The class which enclosed nested class is known as Outer class. Now let's come back to static vs non static class. In Java programming language you can not make a top level class static. You can only make nested class either static or non static. If you make a nested class non static then it also referred as Inner class. Now let's come to the point regarding Difference between static and non static nested class in Java
Difference between static and non static nested class in Java
1) Nested static class doesn't need reference of Outer class but non static nested class or Inner class requires Outer class reference. You can not create instance of Inner class without creating instance of Outer class. This is by far most important thing to consider while making a nested class static or non static.
2) static class is actually static member of class and can be used in static context e.g. static method or static block of Outer class.
3) Another difference between static and non static nested class is that you can not access non static members e.g. method and field into nested static class directly. If you do you will get error like "non static member can not be used in static context". While Inner class can access both static and non static member of Outer class.
Here is the code sample of using both nested static class vs non static class :
/**
* Java program to demonstrate What is nested static and non static class.
* How to create instance of static and non static class and How to call
* methods of nested static and Inner class in Java. Overall comparison of
* static vs non static class.
*/
class Outer{
private static String message = "HelloWorld";
// Static nested class
private static class MessagePrinter{
//Only static member of Outer class is directly accessible in nested static class
public void printMessage(){
// Compile time error if message field is not static
System.out.println("Message from nested static class : " + message);
}
}
//non static nested class - also called Inner class
private class Inner{
// Both static and non static member of Outer class is accessible in this Inner class
public void display(){
System.out.println(" Message from non static nested or Inner class : " + message);
}
}
// How to create instance of static and non static nested class
public static void main(String... args){
// creating instance of nested Static class
Outer.MessagePrinter printer = new Outer.MessagePrinter();
//calling non static method of nested static class
printer.printMessage();
// creating instance of non static nested class or Inner class
// In order to create instance of Inner class you need an Outer class instance
Outer outer = new Outer(); //outer class instance for creating non static nested class
Outer.Inner inner = outer.new Inner();
//calling non static method of Inner class
inner.display();
// we can also combine above steps in one step to create instance of Inner class
Outer.Inner nonStaticIner = new Outer().new Inner();
// similarly you can now call Inner class method
nonStaticIner.display();
}
}
Output:
Message from nested static class : HelloWorld
Message from non static nested or Inner class : HelloWorld
Message from non static nested or Inner class : HelloWorld
That's all on Difference between Static and non Static nested class in Java. So far we have only touched member Inner class and not discussed other two types on Inner class e.g. Local class and Anonymous Inner class. In this Java tutorial we have seen What is nested static class in Java and How to create instance of both nested static and non static class in Java.
In summary its easy to create instance of nested static class as it doesn't require instance of Outer class while non static nested class e.g. Inner class will always need an Outer class instance and can not exist without Outer class. If you have to choose between static vs non static class than prefer static nested class if you can use that.
0 comments:
Post a Comment