Site icon JavaGoal

Static class in java

The static keyword in java is the most important and confusing topic. We have already discussed the static keyword in java in a separate post. We have seen the use of a static keyword to make the static variable, static method, and static block. Now we will see how to use a static keyword with the class that is known as a static class in java or How static class does work?
Most programmer knows about the static class but only a few of them know “when we should use the static class? or Why we should use a static class?”
In this post, we will cover them in this post.

Here is the table content of the article will we will cover this topic.
1. What is the static class in java?
2. Properties of static class?
3. Declaring a Static Class?
4. How to create an object of static class?
5. Accessing Static Class Members from Static nested Classes?
6. How does static class work?
7. Key Points to Remember?
8. What is the need of static class?

What is the static class in java?

In java, when we use the static keyword with a class is known as a static class. But we can’t use the static keyword with a class unless it is a nested class. It means there is nothing called static class, we only have only a static nested class java or static inner class. To understand the concept of static class we need to understand the nested classes first. Because static class is part of nested classes.

What is the Nested class in java?

We can declare a class inside of another class such classes are known as nested classes. A class which defined inside another class is known as an inner class and the class that contains it is known as an outer class. The Inner Class doesn’t exist without the Outer Class. It totally depends on the Outer Class. The scope of an Inner Class is bounded by the scope of its enclosing class. You can’t access the Inner Class without using the Outer Class. If you haven’t read the nested classes yet, then you should read them first. We have covered nested classes in a separate topic. you can read it in detail. Now we will move to static class again.

In Java, we can create a static class, but it should be an inner class. A static class can’t be the outer class, Java doesn’t permit it. If we try to create an outer class as a static class, then it will show a compilation error.

It is also known as a static inner class or static nested class. A static inner class is a nested class that is a static member of the outer class. Unlike other nested classes, A static inner class can be accessed without instantiating the outer class.

NOTE: A static class is also known as a static nested class or static inner class.

Properties of static class

1. Must be a nested class

In Java, a static class must be a nested class we can’t declare a static class like a normal class. It must be an inner class. Due to this reason, static class always appears somewhere in the middle or bottom within a class.

2. Independent

As you know in nested classes, the inner class reference always depends upon the outer class reference. But it behaves differently in the case of a static class (although it’s a nested class), the static class doesn’t need the reference of the outer class. It can access directly and act as an outer class within itself.

3. Access only static member

In nested classes, an inner class instance can access all of the members of the outer class.  All the non-static inner classes can access both static and non-static members of the outer class. But a static nested class can access only the static members of the outer class.

Declaring a Static Class

We can declare a class as static by the use of the static keyword. But we can’t use static keyword with a normal class, it must an inner class.

class OuterClassName
{
        // Body of outer class
        static class InnerClassName
        {
            // Body of outer class
        }
}
class Car
{
        // Body of outer class
        static class Wheel
        {
            // Body of outer class
        }
}

Here we have created a class Car that is considered an Outer class. A static class Wheel is also created as an inner class. The keyword static ensures that class Wheel will always be used as the outer class(Car) only. The static class(Wheel) can access only static members of an outer class.

public class Car
{
    public static class Wheel 
    {
        public Wheel()
        {
            System.out.println("Constrcutor of Wheel class");
        }
    }

    public Car() 
    {
        System.out.println("Constrcutor of  Car class");
    }
    public static void main(String[] args) 
    {
    	Car car = new Car();
    }
}

Output: Constructor of  Car class

In the above example, I have added constructors to both classes. So that we can understand it in a better way and see when they are instantiated. When JVM invokes the constructor of the Car class then it is not invoking the constructor of the Wheel class(inner class).

How to create an object it?

You must have seen in the above example when we are creating the object of the Car class the JVM is not creating the object of the Wheel class(static class). It means when we create an object of the Outer class, the JVM doesn’t invoke the constructor of the inner class. To create an object of the static class, we need to use the name of the outer class.

OuterClassName.staticClassName obj = new OuterClassName.staticClassName();
public class Car
{
    public static class Wheel 
    {
        public Wheel()
        {
            System.out.println("Constructor of Wheel class");
        }
    }

    public Car() 
    {
        System.out.println("Constructor of  Car class");
    }
    public static void main(String[] args) 
    {
    	Car car = new Car();
    	Car.Wheel wheel =  new Car.Wheel();
    }
}

Output: Constructor of  Car class
Constructor of Wheel class

Accessing Members from Static nested Classes

We have discussed the properties of static class in a separate section. We have learned a static inner class can access only the static data members of the outer class.  It can’t access non-static data members of the outer class. If you want to access all the members (static or non-static ), you have to create an instance of a static inner class.

public class Car
{
	static int a = 123;
	int b = 456;
	
	private static String showData()
	{
		return "Static method of Car class";
	}
	
	private String getData()
	{
		return "Non-static method of Car class";
	}
	
    public static class Wheel 
    {
        public Wheel()
        {
            System.out.println("Static member can access directly");
            System.out.println("Accessing the static variable : "+ a);
            System.out.println("Accessing the static method : "+showData());
            
            Car car = new Car();
            System.out.println("Accessing the non-static variable : "+ car.b);
            System.out.println("Accessing the non-static method : "+ car.getData());
        }
    }

    public Car() 
    {
        System.out.println("Constructor of  Car class");
    }
    public static void main(String[] args) 
    {
    	Car car = new Car();
    	Car.Wheel wheel = new Car.Wheel();
    }
}

Output: Constructor of  Car class
Static member can access directly
Accessing the static variable : 123
Accessing the static method : Static method of Car class
Constructor of  Car class
Accessing the non-static variable : 456
Accessing the non-static method : Non-static method of Car class

How does the static class work?

As you know the compiler compiles .java files and creates .class files that contain the byte codes. The .class always has the same name as .java file. But the compiler does work differently in the case of the nested class. The compiler creates two class files in the case of an inner class. One class file for the Outer class and another for the Inner class. The class file name of the inner class is “Car$Wheel”

Key Points to Remember

1. A static class(inner class) is treated as a regular member of a class. Because a static class is always declared as an inner class. The static class(inner class) are like as methods and variables declared inside an outer class.

2. We can apply any access modifier with a static class because it is an inner class. We can use private, protected, or public access modifiers which is not possible in normal classes.

3. By use of static class, it will make our code more readable and provide better encapsulation.

What is the need of static class?

We have already discussed a static class is part of the nested classes. We can’t create a normal class as a static class. But we should understand the need for static classes.

1. By use of static nested class we can achieve a logical grouping of classes. It means if a class is useful only for one other class, then we can embed it in that class and keep the two together. Now you must think we can use inheritance also to use the functionality of other classes. But here we will discuss how nested classes are over to inheritance.

2. The static nested class (nested classes) has a relationship that binds the outer class and inner class. The inner class can access all the members (data members and methods) of the outer class including private ones. But by use of inheritance, we can’t use the private data of the subclass.

3. It provides a more readable and maintainable code because we can place the code in one place. When we know that a class is useful only for a single class so it would be a better choice to place them together.

4. The static class(nested class) increases the encapsulation. Suppose we have two classes that are OuterClass and InnerClass.  The InnerClass can access the members of OuterClass even they are private members. By hiding InnerClass within OuterClass, In OuterClass We can declared private member and InnerClass can access them.

We can take a real-time example for our JDK. Here we have a class HashMap that has a static class Node. This class binds with the HashMap class.

Exit mobile version