Static members

Fields and methods can be declared to be static. In the case of fields, this means that the value of the field is associated with, rather than an instance of the class, the class itself. The utility of this is two-fold. One is that it enables you to make a value available without requiring an instance to be created. As an example, Java makes the value of π available as a static field of the Math class: Math.PI. You refer to the value of the static field by naming the class, followed by “.”, followed by the field name.

To declare a field static, put the keyword static before the type of the field.

class BattleshipGame {
    static int GRID_SIZE = 10;
    ...
}

This variable can be accessed as BattleshipGame.GRID_SIZE. Another good example would be the KILOMETERS_PER_MILE field of the Distance class:

class Distance {
   double value;
   Units units;
   static double KILOMETERS_PER_MILE = 1.609;
   ...
}

A second utility of a static field is that there is only copy of it in your program. When you run your program, Java loads it into memory, which means loading all your class definitions and enumerated type definitions into memory. You can think of static fields as living with this class definition. When you make an instance of one of your classes, using the new operator, you are making new copies of all non-static fields, but you are not making new copies of the static fields. There is always only one copy of a static field, and it lives with the class definition. The utility of this is that you can use this “singleton” value, which is available everywhere in your program, to hold information that may be required throughout your program. We'll save an example of this for later, when it will make more sense.

Static fields are also known as “class variables.” (Recall that non-static fields are known as “instance variables.”)

Methods, too, can be declared as static. (A static method is also known as a “class method.”) As with fields, this means that the method is associated with the class definition rather than an instance of the class. To invoke the method, you only need to know the name of the class; you do not need an instance of the class. The beats and tie methods of the RockPaperScissors class could be static methods, because they do not use instance variables; they depend completely on the parameters.

class RockPaperScissorsGame {
  int player1Score;
  int player2Score;
  
  static boolean beats ( Choice player1Choice, Choice player2Choice )
  {
    return 
      player1Choice == Choice.ROCK && player2Choice == Choice.SCISSORS
      || player1Choice == Choice.PAPER && player2Choice == Choice.ROCK
      || player1Choice == Choice.SCISSORS && player2Choice == Choice.PAPER;
  }
  
  static boolean tie ( Choice player1Choice, Choice player2Choice )
  {
    return player1Choice == player2Choice;
  }    
}
    
enum Choice { ROCK, PAPER, SCISSORS }

When a method doesn't use instance variables and can therefore be static, then it is a good idea to make it static. Since it doesn't require an instance for its function, it shouldn't require an instance in order to be invoked.