DOC PREVIEW
UMD CMSC 131 - Lecture 12: Static Methods and Variable

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 MethodsStatic 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 MethodsIf 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

UMD CMSC 131 - Lecture 12: Static Methods and Variable

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 12: Static Methods and Variable
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 12: Static Methods and Variable and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 12: Static Methods and Variable 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?