6.7. Static Variables and Methods

In Unit 2, we explored the Math class and its many static methods like Math.random(), and we’ve always used a main method which is static. In this lesson, you will learn to write your own static variables and methods.

class ClassName {
  // static variable
  public static type variableName;

  // static method
  public static returnType methodName(parameters) {
        // implementation not shown
  }
}
// To call a static method or variable, use the Class Name
System.out.println(ClassName.staticVariable);
ClassName.staticMethod();

Static methods only have access to other static variables and static methods (unless the static method creates an object through which to access instance variables and methods). Static methods cannot access or change the values of instance variables or use the “this” reference (since there is no calling object for them), and static methods cannot call non-static methods. However, non-static methods have access to all variables (instance or static) and methods (static or non-static) in the class.

Since there is only 1 copy of a static variable or method, static variables are often used to count how many objects are generated. In the following class Person, there is a static variable called personCounter that is incremented each time the Person constructor is called to initialize a new Person object. The static method printCounter() prints out its value. You can also watch how it works in the Java visualizer by clicking the CodeLens button below.

What will the following code print out? Try adding another Person object and see what happens. Try the CodeLens button to run the code step by step.

Another common use for static variables is the keep track of a minimum or maximum value or an average of the values in a collection of objects.

exercise Check Your Understanding

You can see this code in action in the Java visualizer.

coding exercise Coding Exercise

Fix the bugs in the following code.

6.7.1. groupwork Programming Challenge : Static Song and counter

In Unit 5-2, we wrote a class with a static method to print out the verses in the Old MacDonald song. Notice that this is a class where there are no instance variables and we don’t really need to generate multiple objects. With students or pets, it makes sense to have multiple objects. With the Song, we were able to make the verse method static and have just 1 copy.

Add a static variable to the Song class that keeps track of the number of verses. Increment this variable each time the method to print a verse is called and print it out. Update the main method to add a few more verses (pig says oink, chicken says cluck) and rerun the program.

6.7.2. Summary

  • Static methods and variables include the keyword static before their name in the header or declaration. They can be public or private.

  • Static variables belong to the class, with all objects of a class sharing a single static variable.

  • Static methods are associated with the class, not objects of the class.

  • Static variables are used with the class name and the dot operator, since they are associated with a class, not objects of a class.

  • Static methods cannot access or change the values of instance variables, but they can access or change the values of static variables.

  • Static methods cannot call non-static methods.

You have attempted of activities on this page