CodeGym /Java Blog /Java Classes /What is a static class in Java?
Author
Alex Vypirailenko
Java Developer at Toshiba Global Commerce Solutions

What is a static class in Java?

Published in the Java Classes group
Java is an object-oriented programming language, and objects are the main building blocks in Java. However, Java also provides the concept of a static class. A static class is a class that can only contain static methods, variables, and nested classes. In this article, we'll explore what a static class is, how it works, and its implementation in Java.

Static class in Java

A static class in Java is a class that cannot be instantiated. That is, we cannot create objects of a static class. We can only access its members using the class name itself. In other words, a static class is a class that only contains static members. Static classes are often used to group related utility methods together. For example, the Math class in Java is a static class that provides various mathematical operations such as finding the maximum or minimum value, trigonometric functions, and more. We can access the methods in the Math class using the class name and the dot operator, like this: Math.max(5, 10).

Example

Let's take a look at an example of a static class in Java. Suppose we want to create a class to perform various string operations such as reversing a string, finding the length of a string, and more. In this example, we have created a static class called StringUtils, which contains two static methods: reverse and length. These methods take a string as an argument and return the reversed string and the length of the string, respectively. In the main method, we have created a string str and passed it to the `reverse` and `length` methods of the StringUtils class.

public class StringUtils {

    public static String reverse(String str) {
        StringBuilder sb = new StringBuilder(str);
        return sb.reverse().toString();
    }

    public static int length(String str) {
        return str.length();
    }

    public static void main(String[] args) {
        String str = "Hello, World!";
                    
        String reversedStr = StringUtils.reverse(str);
        int strLength = StringUtils.length(str);
        System.out.println(reversedStr);
        System.out.println(strLength);
    }
}
The output of the program will be:
!dlroW ,olleH 13
In the next updated example, we have added a static nested class called Formatter within the StringUtils class. This static nested class also contains a static method called format that takes a string and returns its uppercase version. What is a static class in Java? - 1In the main method, we have used the static nested class Formatter to format the string str and print it to the console. We have also called the reverse and length methods of the StringUtils class to reverse the string and find its length, respectively.

public class StringUtils {

    // nested Static class
    public static class Formatter {
        public static String format(String str) {
            return str.toUpperCase();
        }
    }

    public static String reverse(String str) {
        StringBuilder sb = new StringBuilder(str);
        return sb.reverse().toString();
    }

    public static int length(String str) {
        return str.length();
    }

    public static void main(String[] args) {
        String str = "Hello, World!";
        
        // Using static class
        String formattedStr = StringUtils.Formatter.format(str);
        System.out.println(formattedStr);
        
        String reversedStr = StringUtils.reverse(str);
        int strLength = StringUtils.length(str);
        System.out.println(reversedStr);
        System.out.println(strLength);
    }
}
The output of the program will be:
HELLO, WORLD! !dlroW ,olleH 13
As you can see, static classes in Java can also contain static nested classes, which can be used to group related functionality together.

Conclusion

In conclusion, a static class in Java is a class that can only contain static methods, variables, and nested classes. We cannot create objects of a static class and can only access its members using the class name itself. Static classes are often used to group related utility methods together. In the example above, we have seen how to create a static class and use it to perform various string operations.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION