Lecture 12 Static Methods and Variables Last time 1 Aliasing 2 Constructors Today 1 for loops from last lecture notes 2 Project from last lecture notes 3 Static variables and methods 9 27 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Static 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 which are shared by all objects in a class CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Why 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 help CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Declaring Static Variables and Methods Static variables public static int foo 1 Static methods public static void main CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Example Object Counting public 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 2006 Rance Cleaveland 2006 University of Maryland 4 What 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 2 2 3 3 3 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Class Access to Static Variables and Methods If C is a class sv is a static variable and sm is 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 2006 Rance Cleaveland 2006 University of Maryland 6 What 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 2 2 2 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 When To Use Static Variables Class wide constants static final int MAX ENROLLMENT 25000 Class wide aggregate data static int numStudents 0 Be careful about aliasing effects Student s1 new Student Student s2 new Student s1 numStudents 15 Changes s2 numStudents CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 When 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 nonstatic methods CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9
View Full Document