This is one of the tricky questions in Java because there is no straightforward. 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 to 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 the top-level if it is not inside any other class, and a class that is inside another class is known as a nested class. You can create a nested class inside another top-level or another nested class in Java. This is the class that can be static in Java.
Some of you must be wondering what the benefit of making a class static in Java is? Why would one make a class static? In order to answer this question, let's revise the concept of what does static means 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 the class name, i.e. you don't need to create an instance of the class to call this method because it belongs to a class.
The same is true with static member variables as well; you can access them without creating an instance of the class. They can be accessed by using the class name, like Class.variable.
A non-static nested class, also known as the Inner class on the other hand always needs 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.
Anyway, This challenge can be overcome by declaring the nested class static in Java i.e. you can directly create an instance of a nested class without first creating an object of enclosing class. This is the single most important reason why you should make a nested class static in Java, and this is what was 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.
In Eclipse, as soon as type the code, I receive the 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.
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 a nested static class over a non-static class in Java because of convenience (see Effective 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 classes from JDK.
Some of you must be wondering what the benefit of making a class static in Java is? Why would one make a class static? In order to answer this question, let's revise the concept of what does static means 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 the class name, i.e. you don't need to create an instance of the class to call this method because it belongs to a class.
The same is true with static member variables as well; you can access them without creating an instance of the class. They can be accessed by using the class name, like Class.variable.
A non-static nested class, also known as the Inner class on the other hand always needs 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.
Anyway, This challenge can be overcome by declaring the nested class static in Java i.e. you can directly create an instance of a nested class without first creating an object of enclosing class. This is the single most important reason why you should make a nested class static in Java, and this is what was 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.
1.Making a 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 that 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 a compile-time error because this is not permitted in Java:static class Top{
...
}
In Eclipse, as soon as type the code, I receive the 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.
2. 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 that 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 a nested static class over a non-static class in Java because of convenience (see Effective 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 classes from JDK.
By the way, If you are not familiar with the concept of nested class then I also suggest you join a comprehensive Java course like The Complete Java Masterclass on Udemy. This is the best Java course on Udemy and also the most up-to-date course to cover new features of Java from recent releases.
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 like the java.lang.Math, they are also known as a static class but that is a 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 modifiers in Java you may like
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.
P. S. - If you are new to the Java world and looking for a free online training course to learn Java then you can also check out this free Java Tutorial for Complete Beginners course on Udemy. It's completely free and you just need a Udemy account to join this online course.
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 like the java.lang.Math, they are also known as a static class but that is a 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 modifiers 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 methods in Java?
- How public static final variable works in Java?
- Difference between the 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.
P. S. - If you are new to the Java world and looking for a free online training course to learn Java then you can also check out this free Java Tutorial for Complete Beginners course on Udemy. It's completely free and you just need a Udemy account to join this online course.
thank u
ReplyDeleteNice one.
ReplyDelete