Can you make a class Static in Java?

This is one of the tricky questions in Java because there is no straight forward Yes and No answer here. You cannot make a top level class static in Java but Yes, you can make a nested class static in Java. In fact, it is even advised (see Effective Java) to prefer a nested static class in Java than normal inner classes. Now, the question comes what is a top level class and what is a nested class in Java? Well, you can declare multiple classes in a single Java source file. A class is said to be top level if it is not inside any other class and a class which is inside another class is known as nested class. You can create nested class inside another top level or another nested class in Java. This is the class which can be static in Java.

Some of you must be wondering what is the benefit of making a class static in Java? Why would one make a class static? In order to answer this question, let's revise the concept of what does static mean in Java? Well, a static keyword can be used with a class, method or variable in Java and anything static belongs to the class, which means a static method can be accessed and called just by using class name i.e. you don't need to create an instance of the class to call this method because it belongs to class.

Same is true with static member variable as well, you can access them without creating any instance of the class. They can be accessed by using class name e.g. Class.variable.

A non-static nested class, also known as the Inner class on the hand always need an instance of enclosing class, which means you cannot create an instance of the nested class without first creating an instance of the outer class.

This challenge can be overcome by declaring the nested class static in Java i.e you can directly create an instance of nested class without first creating an object of enclosing class.


This is the single most important reason for why you should make nested class static in Java and this is what advised by Joshua Bloch in his all time classic Java book, Effective Java.

Like any other core Java question, we should verify if compiler support theses fact or not. To check whether we can make a class static in Java or not, we'll try both top level and a nested class static.


Using static modifier with a top level class

Let's try to use the static modifier with a top level class i.e. a class which is not inside any other class. Here is the code to do that, when you compile this code in Java from the command line you will see compile time error because this is not permitted in Java:

static class Top{
 ...
}

In Eclipse, as soon as type the code, I receive following compiler error "Illegal modifier for the class Top; only public, abstract & final are permitted". You will receive something similar when you compile this class using javac from the command line.




Using static modifier with a nested class in Java

Now, let's try to use the static modifier with a nested class in Java i.e. a class which is inside another class. Here is the code to do that and this time you won't see any compile time error. I'll also show you how to create an instance of the nested static class in Java. A nested class is named by joining the name of the top level class and nested class with a dot as shown below:

class Top{

static class Nested{
 ......
}
}

class Test{

public static void main(String args[]){
   Top.Nested n = new Top.Nested(); // ok
}
}

You can see that there is no compile time error this time, which means you can make a nested class static in Java. In fact, it is also a best practice to prefer nested static class over a non-static class in Java because of convenience (see Effective Java).

Can you make a class Static in Java?

As you can see, you don't need an instance of enclosing or top level class to create an instance of the nested static class. Many Java classes follow this practice e.g. LinkedList.Node and Map.Entry is two popular examples of nested static class from JDK.


That's all about whether you can make a class static in Java. A static class is possible in Java but only when if it is nested, you can not make a top level class static in Java. Btw, don't confuse the discussion here with a class with just static methods e.g. java.lang.Math, they are also known as a static class but that is the totally different thing.

All the discussion here about whether you can use the static modifier with a class or not and as I explained, you can only use the static modifier with a nested class in Java. Trying to use a static modifier with a top level class in Java.

Other interesting questions about static modifier in Java you may like
Can you overload a static method in Java?
Can you override a static method in Java?
Difference between the static and non-static method in Java?
How public static final variable works in Java?
Difference between constructor and static factory method?
Difference between static and dynamic binding?

Thanks a lot for reading this article so far. If you like this article then please share with your friends and colleagues, if you have any question or feedback then please drop a comment. 

No comments:

Post a Comment