Java

What Does Static Keyword Mean in Java?

Java provides a keyword named static that allows us to access class attributes/variables, or methods, without creating the object of the class to which it belongs. In java, class-level variables, methods, blocks, and inner classes can be created using a static keyword while outer classes and local variables can’t be created as static.

In this write-up, we will explore the following aspects of the static keyword in java:

So, let’s get started!

Static Keyword in Java

It describes that the specific variable/method belongs to a class itself, rather than the instance of that class. The static methods and variables can be invoked/called directly with the class name to which they belong. So, all in all, if you don’t want to make the members/methods object-specific then you can use the static keyword.

What are Java Static Variables

Any variable/class attribute declared with a static keyword is referred as a static variable in java. Only class-level variables can be declared as static variables.

Syntax

The below snippet shows the basic syntax of static variables:

static int age;

From the above snippet, we have learned that to declare a static variable firstly we have to use the static keyword followed by data type, and then we have to specify the variable name.

Why Static Variables

As of now, we are aware of what are static variables in java? But what is the need for static variables, and why should someone create and use static variables in java?

The static variables assist us in memory management as they get the memory only once.

Example

Let’s understand the significance of static variables with the help of a real-world scenario:

Let’s say there are two hundred employees in an IT company, now whenever an object is created all the instance data members get the memory each time.

The instance data member is good enough for the attributes that are unique such as employee id, name, etc.

However, if we talk about the common attributes it will be better to declare them as static so that such field gets the memory only once.

For example, the CEO of the company will remain the same for all the employees, so it should be declared as static.

Can we Use a Static Keyword with Local Variables?

No, this is because a local variable has a limited scope and if we declare a local variable as static then it violates/breaches the purpose of static therefore java doesn’t allow static local variables.

What are Java Static Methods

Any method declared with a static keyword is referred as a java static method. These methods can access the static data members and can modify their values. The methods declared with a static keyword can be invoked directly with the class name to whom they belong.

Syntax

The basic syntax of the static method is shown in the following snippet:

static void fun()
{
//code
}

From the above snippet, we have learned that to declare a static method firstly we have to use the static keyword followed by return type, and then we have to specify the method name.

Before heading towards how to use static variables and methods in Java, let’s first understand what will happen if we try to access non-static variables and methods using the class name instead of class object:

class Employee {

    String name = "Joe";
    int age = 32;

    void showName() {
        System.out.println("Employee Name: " + name);
    }
}

public class StaticKeywordExample {

    public static void main(String[] args) {
        Employee.showName();
        System.out.println("Employee Age: " + Employee.age);
    }

}

The complete code and respective output will be:

From the above snippet, it is clear that we can’t access the non-static variables and methods directly with the class name.

How to Use Static Variable and Methods in Java

As of now, we have basic knowledge of what are static variables and methods and now it’s time to understand how to create and use them in java. So, let’s consider an example for a profound understanding of this concept.

Example:

Now this time we declare the variables and methods as static and access them using the class name.

class Employee {

    static String name = "Joe";
    static int age = 32;

    static void showName() {
        System.out.println("Employee Name: " + name);
    }
}

public class StaticKeywordExample {

    public static void main(String[] args) {
        Employee.showName();
        System.out.println("Employee Age: " + Employee.age);
    }

}

The below-given shows how to use a static keyword with variables and methods:

Now this time everything goes fine and we succeed in accessing the variables and methods of Employee class directly with the class name; hence it authenticates the working of static keyword.

Static Inner Class in Java

A java class that is created within some other class is referred as a java inner class and the inner class created with a static keyword is referred as a static inner class.

Syntax

The basic syntax of the static inner class is shown in the below-given snippet:

class Outer {
static class Inner {
//code
    }
}

From the above snippet, we have learned that to create a static inner class initially we have to use static keyword followed by a class keyword, and then we have to specify the class name.

Example

Let’s consider the below-given code snippet for a profound understanding of how to create a static inner class.

public class OuterClass {

    static int id = 32;

    static class Employee {

        static void show() {
            System.out.println("Employee id: " + id);
        }
    }

    public static void main(String[] args) {
        Employee.show();
    }

}

In this example, we have an outer class named “OuterClass” and a static inner class named “Employee”. Within the outer class we have a static variable age whose value will be fetched and displayed in the show() method of a static inner class “Employee”:

So, from the above snippet, it is clear that we can access the variables and methods of a static inner class directly with the class name.

Can we Use Static Keyword with Java Outer Class?

The purpose of declaring any member as static is that the member should be common to all objects of the class. Now, if we talk about the outer class, it is already common and visible to all the objects same as static modifiers so, there is no need to declare them as static.

Conclusion

In java, the static keyword is used to create the static methods, variables, etc. and these static variables and methods can be accessed directly with the class name, so there is no need to create the class’s object to which they belong. In java, class-level variables, methods, blocks, and inner classes can be created as static while outer classes and local variables cant be created as static. This write-up explains different aspects of the static keyword in java for example what is a static keyword? where it can be used? And what is the purpose of using a static keyword in java?

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.