What Does “Static” Mean in Java?

What Does “Static” Mean in Java?

·

7 min read

Play this article

Static is a keyword (meaning that it has a special meaning for the compiler), meaning that members of a class (a variable or a method) belong to the class itself.

This means that you don't need to create an object to access a class member.

To be more precise, when you declare a class member as static, only a single instance of that member gets created and distributed among all objects.

You don't need to use the new keyword normally used to create an object to access that static member.

The static keyword applies to variables, methods, blocks and nested classes.

Static keyword on variables

Let's imagine this scenario:

You want to create a sample of an employee management system for Tesla. This system does many things, but we want to ensure it successfully displays all its employees accompanied by the company name.

You can ensure all employees will have the company name "Tesla" in common.

Therefore, you can make Tesla static because all employees will share it.

Let's look at the example below:

public class Employee {

    private String employeeName;
    private static String companyName = "Tesla";

    public Employee(String employeeName) {
        this.employeeName = employeeName;
    }

    public void displayEmployee() {
        System.out.println("Employee " + employeeName + " is employed at " + companyName);
    }
}

This employee class has the following:

  • an employee name.

  • the company name (Tesla).

  • a constructor where you're going to pass the name.

  • a void method that prints out both the employee name and the company name.

Then, you create a separate class with the main() method to run the application.

public class EmployeeManagementApplication {

    public static void main(String[] args) {

        Employee employeeOne = new Employee("Lisa");
        Employee employeeTwo = new Employee("Jennifer");
        Employee employeeThree = new Employee("Beatrice");

        employeeOne.displayEmployee();
        employeeTwo.displayEmployee();
        employeeThree.displayEmployee();
    }
}

The result is:

Employee Lisa is employed at Tesla
Employee Jennifer is employed at Tesla
Employee Beatrice is employed at Tesla

Tesla is the company name in common among Lisa, Jennifer, and Beatrice.

If you were going to make the company name non-static, we would have to passed "Tesla" in the constructor.

public class Employee {

    private String employeeName;
    private String companyName = "Tesla";

    public Employee(String employeeName, String companyName) {
        this.employeeName = employeeName;
        this.companyName = companyName;
    }

    public void displayEmployee() {
        System.out.println("Employee " + employeeName + " is employed at " + companyName);
    }
}
public class EmployeeManagementApplication {

    public static void main(String[] args) {

        Employee employeeOne = new Employee("Lisa", "Tesla");
        Employee employeeTwo = new Employee("Jennifer", "Tesla");
        Employee employeeThree = new Employee("Beatrice", "Tesla");

        employeeOne.displayEmployee();
        employeeTwo.displayEmployee();
        employeeThree.displayEmployee();
    }
}

The result is:

Employee Lisa is employed at Tesla
Employee Jennifer is employed at Tesla
Employee Beatrice is employed at Tesla

However, you're using more memory by making the company name non-static.

The static keyword is ideal for memory management because you'll only have to declare it once for all objects.

Static keyword on methods

Let's say that, instead of just having "Tesla", you want to include the company suffix "Inc" (= Incorporated).

All employees will have the suffix applied to the company name.

We can create a static method to change the company name from "Tesla" to "Tesla Inc".

public class Employee {

    private String employeeName;
    private static String companyName = "Tesla";

    public Employee(String employeeName) {
        this.employeeName = employeeName;
    }

    public void displayEmployee() {
        System.out.println("Employee " + employeeName + " is employed at " + companyName);
    }

    public static void addSuffixToCompanyName() {
        companyName = "Tesla Inc";
    }
}

In this class, we added a static method addSuffixToCompanyName(), which will add the suffix to the company name.

In the main method, we'll have this:

public class EmployeeManagementApplication {

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

        Employee employeeOne = new Employee("Lisa");
        Employee employeeTwo = new Employee("Jennifer");
        Employee employeeThree = new Employee("Beatrice");

        employeeOne.displayEmployee();
        employeeTwo.displayEmployee();
        employeeThree.displayEmployee();
    }
}

The result is:

Employee Lisa is employed at Tesla Inc
Employee Jennifer is employed at Tesla Inc
Employee Beatrice is employed at Tesla Inc

Two things to keep in mind when it comes to static methods:

  1. You cannot use non-static members in a static method.

  2. "This" and "Super" cannot be used within a static method because these keywords typically relate to an instance variable. I found this discussion interesting if you'd like to read more.

Static keyword on blocks

Let's say that you want to add the country of employment, "USA".

You can use a static block to do that.

A static block is used when we want to initialize a static member. A static block gets executed when the class gets loaded. I found an interesting discussion on StackOverflow.

     private static String countryOfEmployment;

     static {
         countryOfEmployment = "USA";
     }

The Employee class looks like this:

public class Employee {

     private String employeeName;
     private static String companyName = "Tesla";
     private static String countryOfEmployment;

     static {
         countryOfEmployment = "USA";
     }

    public Employee(String employeeName) {
        this.employeeName = employeeName;
    }

    public void displayEmployee() {
        System.out.println("Employee " + employeeName + " is employed at " + companyName + " in the " + countryOfEmployment);
    }

    public static void addSuffixToCompanyName() {
        companyName = "Tesla Inc";
    }
}

The main method is going to stay the same:

public class EmployeeManagementApplication {

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

        Employee employeeOne = new Employee("Lisa");
        Employee employeeTwo = new Employee("Jennifer");
        Employee employeeThree = new Employee("Beatrice");

        employeeOne.displayEmployee();
        employeeTwo.displayEmployee();
        employeeThree.displayEmployee();
    }
}

The result is:

Employee Lisa is employed at Tesla Inc in the USA
Employee Jennifer is employed at Tesla Inc in the USA
Employee Beatrice is employed at Tesla Inc in the USA

Static keyword on nested classes

Let's imagine you want to link an employee with their work details, such as their department, the contract type, and the contract's basis.

Create a static nested class called WorkDetail.

     static class WorkDetail {
         String department = "Engineering";
         String contractType = "Permanent";
         String contractBasis = "Full time";

         public void displayWorkDetail() {
             System.out.println("Employees are employed in the "
                     + department
                     + " department "
                     + " on a "
                     + contractType
                     + " and "
                     + contractBasis
                     + " basis ");
         }
     }

The entire class would look like this:

public class Employee {

     private String employeeName;
     private static String companyName = "Tesla";
     private static String countryOfEmployment;

     static class WorkDetail {
         String department = "Engineering";
         String contractType = "Permanent";
         String contractBasis = "Full time";

         public void displayWorkDetail() {
             System.out.println("Employees are employed in the "
                     + department
                     + " department "
                     + " on a "
                     + contractType
                     + " and "
                     + contractBasis
                     + " basis ");
         }
     }

     static {
         countryOfEmployment = "USA";
     }

    public Employee(String employeeName) {
        this.employeeName = employeeName;
    }

    public void displayEmployee() {
        System.out.println("Employee " + employeeName + " is employed at " + companyName + " in the " + countryOfEmployment);
    }

    public static void addSuffixToCompanyName() {
        companyName = "Tesla Inc";
    }
}

To use the static nested class, we call it through the outer class (Employee).

Outerclass.StaticNestedClass objectName = new Outerclass.StaticNestedClass();

In our case, the outer class is Employee, and the static nested class is WorkDetail. So it would be:

Employee.WorkDetail employeeWorkDetail = new Employee.Workdetail();
public class EmployeeManagementApplication {

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

        Employee employeeOne = new Employee("Lisa");
        Employee employeeTwo = new Employee("Jennifer");
        Employee employeeThree = new Employee("Beatrice");

        employeeOne.displayEmployee();
        employeeTwo.displayEmployee();
        employeeThree.displayEmployee();

        Employee.WorkDetail employeeWorkDetail = new Employee.WorkDetail();

        employeeWorkDetail.displayWorkDetail();
    }
}

The result would be:

Employee Lisa is employed at Tesla Inc in the USA
Employee Jennifer is employed at Tesla Inc in the USA
Employee Beatrice is employed at Tesla Inc in the USA

Employees are employed in the Engineering department  on a Permanent and Full time basis

Why would you use a static keyword?

The main goal of using the static keyword is to save memory.

When you create a static variable, that variable is accessed across all classes. Using the static keyword saves memory because the variable is created only once.

Additional references

Conclusion

This article covers the static keyword in Java. I tried to come up with the best examples.

I hope you've found this helpful.

Until next time! 👋🏾

Did you find this article valuable?

Support Maddy by becoming a sponsor. Any amount is appreciated!