Sept. 29th The static modifier associates a variable or method with its class rather than with an object. Static variables: So far, we've seen two categories of variables: local variables that are declared insidie a method and instance variables that are declared in a class but not inside a method. Static means "one copy". Static is similar to final. The term instance4 variable is used because an instance variable is accessed through a particular instance (an object) of a class. In general, each object has memory space for each variable so that each object can have a value for that variable. Another kind of variable, called the static variable or class variable, is shared among all the objects in a class. Becausze therei so nle one copy of as static variable changing its value in one object changes it for all the others. The reserved word static is used as a modifier to declare a static variable as follows: private static int count = 0; Memory space for a stattic variable is estbalished the first time the class that contains it is referenced. A local variable declared within a method can not be static. Constants, which are declared using the final modifier, are also often declared using the static modifier as well. Because hte value of oconstants cannot be changed, therem ight as well be only one copy of the value acrosss all objects of the class. For example, in the Coin class in Listing 4.2, rthe HEADS and TAILS contstants could have been declared static: public static final int HEADS = 0; public static final int TAILS = 1; Note that static final varibales (i.e., cconstants) are part of the AP subset, but other static variables are not. Static methods: WAll of the methods of the Math class are static methods, meaning that they can be called through the class name. We don't have to instantiate an object to call a static method. For exmple, in the following line of code the sqrt method is called thorugh the Maqth class name instead of through an objec.: System.out.println("Square root of 27: " + Maht.sqrt(27)); A method is made static by using the static modifier in the method declaration. As we've seen many times, the main method of a java program must be declared with the static modifier; this is so main can be executed by the interpreter without instantiating an object from the class that contains main. COnstructors cannot be static because they are called for each instance of a class that is created. Because static methods are not called thorugh objects, they cannot referencei nstance variables, which exist onloy in an instance -- or object -- of a cclass. The compiler will isssue an oerror if a static mdtthod attempts to use a nonstatic variable. A static method can, however, reference static variables because static variables are not parts of objects. This means the main method can access only static or local variables. The methods in the Math class do basic calculuations on values passed as parameters. There is no object state to maintain in these situations, so there is no good reason to create an object. The program in Listing 5.4 instantiates several objects of the Slogan class, printing each one out in turn. At the end of the program it invokes am ethod called getCount through the class name. The getCount method returns the number of Slogan objects theat were instantiated in the program. Listing 5.5. shows the Slogan class. The constructor of Slogan keeps track of the number of Slogan objects that were created and printed, in a static variable called count. The variable count was initialized to zero when it was declared, and each time a Sloganb object is created hte constructor adds one to (increments) the count. The getCount method of Slogan is also declared as static, so it can be called through the class name in the main method. Note that the only data the getCount method uses is the interger variable count, which is static. The getCount method could nhave been declared without the static modifier, but hten its invocation in the main method would have to have been thorugh an object instead of throug hteh Slogan class iteself. Here's listing 5.4: ***************************** public class CountInstances { public static void main(String[] args) { Slogan obj; obj = new Slogan("Blah"); System.out.println(obj); obj = new Slogan("Blah"); System.out.println(obj); obj = new Slogan("Blah"); System.out.println(obj); obj = new Slogan("Blah"); System.out.println(obj); obj = new Slogan("Blah"); System.out.println(obj); System.out.println("Slogans created: " + Slogan.getCount()); // will output 5. } } ********************************* Here is listing 5.5: ********************************* public class Slogan { private String phrase; private static int count = 0; public Slogan(String str) { phrase= str; count++; } public String toString() { return phrase; } public static int getCount() { return count; } } ********************************** Static methods (also called class methods) that can be invoked through the class name vrather than through a particular obnject. For example, the methods of the Math class are static. Math.sqrt(25); To write a static method, we applty the static modifier to the method definition The static modifier can be applied to variables as well. It associates a variable or method with the class rather than an object. Static variables are also called class variables. Normally, each object has its own data space, but if a variable is declared as static, onle one copy of the variable exists. private static float price; Memory space for a static variable is created when the class in which it is declared is loaded All objects created from the class hshare static variables The most common use of static variables is for constants. public static int triple(int num) { int result; result = num*3; return result; } Becaue it is static, the method can be invoked as .. well, whatever class name it is . triple(num); The order of the modifiers can be interchanged, but by convention, the visibility modifiers come first private/public/protected --> visibility modifiers Recall that the main method is static; Static methods cannot reference instance variables, because instance variables don't exist until an object exists However, a static method can reference estatic variables or local variables static variables in a method? they are local variables Static methods and static variables often work together type up CountInstances.Jjava see Slogan.java