9/27/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 12:Static Methods and VariablesLast time:1. Aliasing2. ConstructorsToday:1. for loops (from last lecture notes)2. Project (from last lecture notes)3. Static variables and methodsCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Static Variables and Methods We have seen how to declare: Instance variables in classes:public int day = 1; Methods in classes:public void setYear (int newYear) { … } Objects created from a class receive their own copies of instance variables and methods Java also has static variables and methods, whichare shared by all objects in a classCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Why Have Static Variables / Methods?Sometimes info needs to be shared among all objects in a class How many objects in a class have been created? A constant that needs to be the same Sometimes it is useful to have methods that can be invoked without first creating objects We will see how static components helpCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Declaring Static Variables and MethodsStatic variablespublic static int foo = 1; Static methodspublic static void main (…) {… }CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Example: Object Countingpublic class Student {public static int numStudents = 0;public String name;public static int getNumStudents () {return numStudents;}Student (String newName) {name = newName;numStudents++;}}CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5What Is Printed? Student s1 = new Student (“John Doe”);Student s2 = new Student (“Mary Roe”);System.out.println (s1.getNumStudents ());System.out.println (s2.getNumStudents ());Student s3 = new Student (“Eduardo Duhalde”);System.out.println (s1.getNumStudents ());System.out.println (s2.getNumStudents ());System.out.println (s3.getNumStudents ()); 22333CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6Class Access to Static Variables and MethodsIf C is a class, sv is a static variable, and smis a static method … Then sv, sm can be accessed via: C.sv C.sm I.e. no object in C needs to be created!CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7What Gets Printed? Student s1 = new Student (“John Doe”);Student s2 = new Student (“Mary Roe”);System.out.println (Student.getNumStudents ());System.out.println (s1.getNumStudents ());System.out.println (s2.getNumStudents ()); 222CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8When To Use Static Variables? Class-wide constantsstatic final int MAX_ENROLLMENT = 25000; Class-wide aggregate datastatic int numStudents = 0; Be careful about aliasing effectsStudent s1 = new Student ();Student s2 = new Student ();s1.numStudents = 15; // Changes s2.numStudents!CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9When To Use Static Methods? When a method should be invocable without object creation When a method should not change instance variables A static method can only change static variables Instance variables can only be changed by non-static
View Full Document