Java Static Class

In this lesson of our Java course, we will learn about the Java static class. We will take a look at why we need static classes in Java with some examples. We will also look at few example to understand the Java static class

1. Java Static Class

A static class in Java is a class that can be instantiated without creating an object of the class. It is a nested class that can access the static members of the outer class. We use the java keyword `static` to define any class as static in Java. A static class in Java is a class that has at least one static member. A static member is a member that belongs to the class rather than to any instance of the class.

Static members can be either static fields or static methods. A static field is a variable that belongs to the class rather than to any instance of the class. A static method is a method that belongs to the class rather than to any instance of the class. Static fields and methods can be accessed using the class name, followed by the dot (.) operator and the name of the field or method

Let’s look at the following code to understand how to create a static class in Java.

public static class StaticClassExample {

    public static final double PI = 3.14;

    public static int multiply(int x) {
        return PI * x;
    }
}

In the above code, we added `static` keyword before the class StaticClassExample to indicate the compiler that it’s a static class. One of the benefit of Java static class is its ability to access its static members without create an object of the class.

double pi = StaticClassExample.PI;

As you can see, we access the PI constant from out StaticClassExample class without creating the object. Similar to the static members, a static class in Java can also contains static method, these methods can also be called without creating any object of the class.

double answer= StaticClassExample.multiply(5.4);
Keep in mind that we need to create object of the class before accessing its method or member if they are not static.

2. Why use Java Static Class

There are many use cases of static classes in Java. Let’s take a look at few of them to get a clear understanding of when to use these classes

  • Utility classes: Java static classes are great use case to create utility classes for common functionality. Let’s take example of data calculation (e.g. converting date from one timezone to another) or String manipulation which is required in every project. We can build a simple utility class with static method to provide the date or string manipulation.
  • Sharing common functionality: If you have a group of classes that share common functionality, you can create a static class with static methods and fields to provide that functionality. This can help reduce code duplication and make your code more modular.
  • Code Performance: It also helps in performance. Every object creation require memory as well computation, since static classes don’t require object creation, it helps to improve the performance of your code by reducing the number of objects that need to be created.
  • We can group the related functionality together which helps in the code readability and maintenance.

Here is one example, where writing a utility class will help us group common functionality and avoid creating objects.

public class FileUtils {
    public static String readFile(String filePath) throws IOException {
        // Code to read file
    }
    
    public static void writeFile(String filePath, String content) throws IOException {
        // Code to write file
    }
    
    public static boolean fileExists(String filePath) {
        // Code to check if file exists

2.1. When not to use static classes

Although, static classes seems a great choice in Java, there are certain cases where we should not use the static classes.

  1. In case we want to create object which can be extended by sub classes.
  2. Avoid using static classes in case object can be used by multiple thread. Remember, static classes and its members are loaded once and shared across.
  3. We should not use static classes where we want to allow user to customize the object creation process.

3. How Does Java Static Class Work?

In Java, when you declare a class as static, it means that the class is loaded into memory as soon as the program starts, even if no instances of the class have been created yet. This is because static members belong to the class and not to any instance of the class.

When a static class is loaded into memory, its static members are also loaded into memory. Static members remain in memory throughout the lifetime of the program, even if no instances of the class are created. This means that you can access the static members of a class even if no instances of the class have been created.

You cannot declare a top-level class as static. You can only declare inner classes as static. An inner class is a class that is defined inside another class. When you declare an inner class as static, it means that the inner class can be accessed without creating an instance of the outer class.

4. Static Class Drawbacks

There are few drawbacks of Static classes in Java and we shuld keep those in mind while trying to use them in our code

  • Static classes are load time and it’s not easy to create instance of the static classes. This will put some challenges especially when we want to create test cases for our code.
  • Debugging static classes in Java is not easy or same as what we do for the normal classes.
  • Not all platform supports Java classes to the same extends which makes the code porting difficult.

Summary

Java static classes are good and useful tool for encapsulating constants, methods, and other static data in Java. They provides us the option to improve code readability and avoid code duplication (think of utility class example). We should use them carefully and they also pose some challenges and have few drawbacks. As Always, the source code for this article is available on our GitHub repository.

Scroll to Top