Create a Static class in Java
Can a class be static in Java?
The answer is YES, we can have a static class in java. In java, we have static instance variables as well as static methods and also static blocks. Classes can also be made static in Java.
Java allows a class to be defined within another class. These are called Nested Classes. The class in which the nested class is defined is known as the Outer Class. Unlike top-level classes, Inner classes can be Static. Non-static nested classes are also known as Inner classes.
You cannot use the static keyword with a class. Java has no way of making a top-level class static but you can simulate a static class like this:
1). Declare your class final - Prevents extension of the class since extending a static class makes no sense.
2). Make the constructor private - Prevents instantiation by client code as it makes no sense to instantiate a static class.
3). Make all the members and functions of the class static - Since the class cannot be instantiated no instance methods can be called or instance fields accessed.
Keep in mind that the compiler will not prevent you from declaring an instance (non-static) member. The issue will only show up if you attempt to call the instance member.
Instantiating a static nested class is different from instantiating an inner class. In the following example shows, how to use a static nested class.
package com.javacodestuffs.core.statics;
public class OuterClass {
static class InnerClass {
public void display() {
System.out.println("Inside nested class...");
}
}
public static void main(String args[]) {
OuterClass.InnerClass inner = new OuterClass.InnerClass();
inner.display();
}
}
output:
Inside nested class...
Static nested class vs Non-static nested class
1). Nested static class doesn’t need a reference of Outer class, but Non-static nested class or Inner class requires Outer class reference.
2). Inner class(or non-static nested class) can access both static and non-static members of the Outer class. A static class cannot access non-static members of the Outer class. It can access only static members of the Outer class.
Uses:The good use of a static class is in defining one-off, utility, and/or library classes where instantiation would not make sense.
Static nested classes can have instance methods and static methods.
Top-level classes:
A java project can contain more than one top-level class in each java source file, one of the classes being named after the file name. There are only three options or keywords allowed in front of the top-level classes, public, abstract, and final.
Inner classes:
classes that are inside of a top-level class are called inner classes, which is basically the concept of nested classes. Inner classes can be static. The idea of making the inner classes static is to take advantage of instantiating the objects of inner classes without instantiating the object of the top-level class. This is exactly the same way as the static methods and variables work inside of a top-level class.
Hence Java Supports Static Classes at Inner Class Level (in nested classes) And Java Does Not Support Static Classes at Top Level Classes.
Post/Questions related to Create Static class in Java
Can you overload or override static method in Java?
Can you override a private method in Java?
Can you run a Java program without the main() method?
Can you make an array volatile in Java?
Can you overload or override the main method in Java?
Can an Abstract class have a constructor in Java?
In this article, we have seen how to Create a Static class in Java with examples. All source code in the article can be found in the GitHub repository.
0 Comments
Post a Comment