DEV Community

Cover image for What does "static" mean in Java?
Maddy
Maddy

Posted on • Edited on • Originally published at techwithmaddy.com

What does "static" mean in Java?

Static is one of those mysterious and by-the-sound "complicated" words that you often read and hear, but you may never understand the meaning of it.

I hope that you'll clear out all the doubts around this topic by the end of this article.

WHAT DOES "STATIC" MEAN IN JAVA?

Static is a keyword (meaning that it has a special meaning for the compiler), and it means 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

✅Nested classes

STATIC KEYWORD ON VARIABLES

Let's imagine this scenario:

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

We can assure that all employees will have in common the company name "Tesla".

Therefore, we 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

This employee class has:

  • an employee name
  • the company name (Tesla)
  • a constructor where we're going to pass the name
  • a void method that prints out both the employee name and the company name

Then, we 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

The result is:

Employee Lisa is employed at Tesla
Employee Jennifer is employed at Tesla
Employee Beatrice is employed at Tesla
Enter fullscreen mode Exit fullscreen mode

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

If we were going to make the company name non-static, we would have to pass 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);
    }
}
Enter fullscreen mode Exit fullscreen mode
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();
    }
}
Enter fullscreen mode Exit fullscreen mode

The result is:

Employee Lisa is employed at Tesla
Employee Jennifer is employed at Tesla
Employee Beatrice is employed at Tesla
Enter fullscreen mode Exit fullscreen mode

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

The static keyword is excellent 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", we 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 that will 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";
    }
}

Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

The result is:

Employee Lisa is employed at Tesla Inc
Employee Jennifer is employed at Tesla Inc
Employee Beatrice is employed at Tesla Inc
Enter fullscreen mode Exit fullscreen mode

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 we want to add the country of employment "USA".

We 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";
     }
Enter fullscreen mode Exit fullscreen mode

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";
    }
}
Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

STATIC KEYWORD ON NESTED CLASSES

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

We can 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 ");
         }
     }
Enter fullscreen mode Exit fullscreen mode

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";
    }
}

Enter fullscreen mode Exit fullscreen mode

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

Outerclass.StaticNestedClass objectName = new Outerclass.StaticNestedClass();
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode
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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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! 👋🏾

Top comments (0)