A nested class is actually a member of a top-level class, so it’s not much difference with a static variable. All instances of the top-level class will have a reference of same nested class if its static, otherwise each of them will refer to a different instance of nested class.
One of the main advantages of making a nested class static is how you instantiate it. Suppose you declare a nested class B inside a top level class A, then it would be referred as A.B and you can create an instance of this class as A.B nestedStaticInstance = new A.B(), unlike Testing.B bs = new Testing(). new B(); for creating an instance of a non-static class, also known as inner class.
So, Yes, you can declare a class static in Java, provided the class is inside a top level class. Such classes are also known as nested classes and they can be declared static, but if you are thinking to make a top level class static in Java, then it’s not allowed. If you do so, the compiler will complain saying “illegal modifier for the class, only public, abstract and final are permitted”.
Now, let’s see some code in action to prove our point. First, let’s try to declare a top level class static in Java and see if we can do it or no.
You can see the compile-time error, it means it is illegal to use the static modifier with a top level class in Java. It doesn’t matter whether the class is public or package and whether its name is same as the name of the Java source file. In short, you cannot make a top level class static in Java i.e. the class which is not inside another class. Here is another example, where I have tried to make a non-public but top level class static in Java:
You can see, still, the same compile time error, “illegal modifier for the class, only public, abstract and final are permitted” is thrown”. If you are hearing about nested or inner class first time, You should first read a good core Java book e.g. Head First Java to learn more about nested class in Java.
Now, let’s try to make a nested class i.e. a class inside the top level class static in Java. As per theory, you can declare a nested class static in Java, let’s see that in code.
This time there is no compiler error, it means you can make a nested class static in Java. I have also shown you how you can create an instance of the nested static class in Java. You can see, you can create an instance without creating an instance of the outer class which was not possible with non-static nested class or inner class.
That’s all about whether you can declare a class static in Java or not. Of course, you cannot make a top level class static in Java but you can always make a nested class static in Java. In fact, it is one of the best practice and also suggested in Effective Java to prefer nested static class instead of non-static inner class.
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at
http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements