Static Methods & Static Instance Variables
Objective #1: Explain the difference between instance methods and static methods in a regular class like Bug, Turtle, or String.
- An instance method is a normal method that applies to an object of a class. After you first instantiate an object in a client program (as in Bug flik = new Bug(); ), you can then call one of its instance methods by typing the name of the method after the dot operator.
For example, the code segment
Bug flik = new Bug();
flik.move();
illustrates the use of the instance method move.
- A static method (also known as a class
method) is one that doesn't apply to a specific object variable. Instead a static method is called by
typing
the name
of the class, followed by the dot operator and then the name of the static method. A
static method has no implicit parameter. If getAirTemperature is a class
method in the Bug class, you would never see the code flik.getAirTemperature() in a client program. Instead, you would use Bug.getAirTemperature(). The
object variable flik cannot be the implicit parameter of the static method
getAirTemperature.
- For example, the code segment
int num1 = 3;
int num2 = 5;
int num = Math.max(num1, num2); // num will be 5
illustrates the use of the static method max which is found in the Math class.
The following attempt to use max as an instance method would cause one or more compile errors:
int num1 = 3;
int num2 = 5;
Math math1 = new Math();
int num = math1.max(3, 5);
- To create a class method within a class, the keyword static must
be used in the method header as in:
public static void maxWord(String string1, String string2)
{
if (string1 > string2)
{
return string1;
}
return string2;
}
Objective #2: Understand that, in a client program, we often use static methods that are typed out below the main method.
- You could include any number of class methods in a client class along with the static method main. For example,
public class Example
{
public static void main(String[] args)
{
intro();
body();
closing();
}
public static void intro()
{
System.out.println("Here is a story");
}
public static void body()
{
System.out.println("of a lovely lady ");
}
public static void closing()
{
System.out.println("The End");
}
}
Objective #3: Explain static instance variables (aka static fields).
- Static fields (also known as static variables, class
variables, or class fields) are instance variables that are shared by all objects of a class. Remember that instance variable and property are synonyms for field so a static field may also be referred to as a static instance variable, static property, class instance variable, or class property.
- Static variables are the opposite of instance variables that we have been using previously in this course. If a Turtle object variable named leonardo has an instance variable named myAge then the value stored in myAge such as 5 would be separate and probably different than the value stored in another Turtle object variable's myAge instance variable. That is, leonardo could have a myAge of 5 while michelangelo could have a myAge that is equal to 7.
But in the case of a static variable such as one named averageTurtleLifeExpectancy, both object variables leonardo and michelango would "share" that static variable. So if you asked michelangelo what his life expectancy is he would give you the same answer as leonardo.
- It is not good style to use static fields but occasionally they are practical to use.
- Static instance variables should be declared as private just
like normal instance variables. Here is an example of a static variable declaration statement (which is more fully explained below):
private static int currentIDNumber = 1;
- The typical situation that you use a static instance variable in this AP course is when you wish to assign a random but unique ID number to each object that is instantiated in a client program.
Let's say you have a client program such as
public static void main(String[] args)
{
Turtle leonardo = new Turtle();
Turtle michelangelo = new Turtle();
Turtle donatello = new Turtle();
. . .
but you want each of the 3 Turtle objects to have a unique ID number (like a Social Security number). In the Turtle, class you might have the following instance variables that include a static instance field named currentIDNumber that is automatically incremented in the constructor like this....
public class Turtle
{
private int myAge;
private int myWeight;
private int myIDNumber;
private static int currentIDNumber = 1;
public Turtle()
{
myAge = 0;
myWeight = 0;
myIDNumber = currentIDNumber;
currentIDNumber++;
}
. . .
In this example, the static instance variable currentIDNumber is initialized to 1 when the first Turtle object (leonardo) is instantiated in the client program. So leonardo's myIDNumber ends up being the value 1. However, since currentIDNumber is incremented, the second Turtle object to be instantiated in the client program (michelangelo) is constructed with a myIDNumber of 2 and the third Turtle object (donatello) is assigned a myIDNumber of 3. In this manner, all Turtle objects are guaranteed to have unique myIDNumber's. Since the keyword static is used in the declaration statement for currentIDNumber, that instance variable retains its value as the client program executes. So you can say that all Turtle objects share (and reuse) this special variable.
- There are three ways to initialize a static field:
- if no value is assigned, the compiler will assign the value 0, false,
or null. This is true for any instance
variable in a class not just static variables.
- an explicit initialization statement such as
private static int currentIDNumber = 1;
- (This syntax is never used on the AP exam) an initialization block such as
public class Game
{
. . .
private static int gameScore;
static
{
gameScore = 0;
}
}
- Constants such as PI in the Math class are static however they are defined as public on purpose so they can be used directly by client programmers. Remember that the modifer keyword final must be used to make something a constant in Java. Here is an example of a constant declaration statement:
public static final int DOZEN = 12;