Wednesday, August 4, 2021

Can we declare a class Static in Java? Top Level and Nested static class Example

The answer to this question is both Yes and No, depending on whether you are talking about a top-level class or a nested class in Java. You cannot make a top-level class static in Java, the compiler will not allow it, but you can make a nested class static in Java. A top-level class is a class that is not inside another class. It may or may not be public like you can have more than one class in a Java source file and only needs to be public, whose name must be the same as the name of the file, rest of the class or interface on that file may or may not be public. On the other hand, a nested class is a class inside a top-level class. It is also known as the inner class or member class in Java.

Now, let's think about what is the meaning of static class, why would someone want to make a class static in Java? If you are familiar with the concept of a static method and static variable in Java, then you know that anything static can be accessed without creating an instance of the class.

For example, you can access the static variable directly as ClassName.variable and you can invoke the static method as ClassName.staticMethod(), this is a great convenience for calling utility method or accessing constants.

This convenience is the main reason Java programmers like to declare a nested class as static in Java. Remember, A nested class, if it's not static then you can't create its instance without first creating an instance of the outer class, which is a bit inconvenient. Such classes are known as the Inner class and they are always associated with an instance of the outer class.

Btw, if you are new to the world of object-oriented programming and design then I also suggest these Software Architecture and Design courses. It's a great resource to understand the complete process of object-oriented analysis and design for creating quality software.




Nested Static Class in Java

A nested class is actually a member of a top-level class, so it's not much different from a static variable. All instances of the top-level class will have a reference of the same nested class if it's static, otherwise, each of them will refer to a different instance of the 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 to 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 an inner class.

So, Yes, you can declare a class static in Java, provided the class is inside a top-level class. Such clauses 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.

How to make a static class in Java


You can see the compile-time error, which 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 the 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:

Can you make a 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 like Head First Java to learn more about nested class in Java.

what is static 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 the 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 a non-static nested class or inner class.


Can we declare a class Static in Java?



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 practices and also suggested in Effective Java to prefer the nested static class instead of the non-static inner class.

Other Interesting questions for Java developers
  • Can you overload or override the static method in Java? (answer)
  • Can you override a private method in Java? (answer)
  • Can you run a Java program without the main() method? (answer)
  • Can you make an array volatile in Java? (answer)
  • Can you overload or override the main method in Java? (answer)
  • Can an Abstract class have a constructor in Java? (answer)
  • Java Programming Interview Exposed by Markham (book)

Thanks for reading this article so far, if you really like this article then please share it with your friends and colleagues. If you have any questions, feedback or suggestion then please drop a comment and I'll try to answer.

2 comments :

Anonymous said...

can instance variables get memory if we do not create object of the class

javin paul said...

For creating variables, yes some memory is assigned on stack but that's minimal compared to actual object.

Post a Comment