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 which is not inside another class. It may or may not be public i.e. you can have more than one class in a Java source file and only needs to be public, whose name must be 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 concept of static method and static variable in Java, then you know that anything static can be accessed without creating 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 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 Inner class and they are always associated with an instance of outer 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.
Other Interesting questions for Java developers
Thanks for reading this article so far, if you really like this article then please share with your friends and colleagues. If you have any question, feedback or suggestion then please drop a comment and I'll try to answer.
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 concept of static method and static variable in Java, then you know that anything static can be accessed without creating 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 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 Inner class and they are always associated with an instance of outer class.
Nested Static Class in Java
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.
Other Interesting questions for Java developers
- Can you overload or override 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 with your friends and colleagues. If you have any question, feedback or suggestion then please drop a comment and I'll try to answer.
No comments:
Post a Comment