DOC PREVIEW
UMD CMSC 131 - Lecture 10: Static Methods and Variables

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 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 11 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 11 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 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)Lecture 10:Static Methods and VariablesLast time:1.Project #2 Hints2.Aliasing3.Constructors, Accessors, Mutators4.Equality5.Printing an object6.for loopsToday:1.for loops (from the previous set of slides)2.Static variables and methodsCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)1Static Variables and MethodsWe have seen how to declare:Instance variables in classes:public int tokenLevel = 3;Methods in classes:public void setName (String nameDesired) { … }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 Spring 2007Jan Plane (adapted from Bonnie Dorr)2Why Have Static Variables / Methods?Sometimes info needs to be shared data among all objects of a specific class typee.g. How many objects in a class have been created?A constant that needs to be the same for all objects of that typeSometimes it is useful to have methods that are in a class that can be invoked without first creating objects of that typeStatic components help for these types of thingsCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)3Declaring Static Methods (and variables and constants)Static methodspublic static void main (…) { … }public static void drawLetter(DrawingGridgrid, char letter, Color color) { … }How do we call static methods?LetterMaker.drawLetter(grid, choice.charAt(0), color)Can have static variables and constants too public static int numStudents = 0;public static final int MAX_ENROLLMENT = 0;How do we use static variables and constants? (see next example)StudentRoster.numStudentsStudentRoster.MAX_ENROLLMENTCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)4Example: Object Countingpublic class StudentRoster {public static int numStudents = 0;public static final int MAX_ENROLLMENT = 50;public static int getNumStudents () {return numStudents;}}…public class Student {<code from previous Student class>Student (String newName) {name = newName;StudentRoster.numStudents++;}}CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)5What Is Printed?Student s1 = new Student (“John Doe”);Student s2 = new Student (“Mary Roe”);System.out.println (StudentRoster.getNumStudents());2Student s3 = new Student (“Eduardo Duhalde”);System.out.println (StudentRoster.getNumStudents());3System.out.println (StudentRoster.MAX_ENROLLMENT);50CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)6Class Access to Static Variables and MethodsIf Ct is a class, sv is a static variable, and smis a static method …Then sv, sm can be accessed via:Ct.svCt.smI.e. no object of type Ct needs to be created at all , but if they do exist that is OK tooCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)7When To Use Static Variables?Class-wide constantsstatic final int MAX_ENROLLMENT = 50;Class-wide aggregate datastatic int numStudents = 0;CMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)8When 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 methodsCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)9Default ValuesStatic and instance variables initializedmost types to 0char to value 0 (non-printable character)strings are assigned to nullLocal variables do not have a default value and you get a error from eclipseCMSC 131 Spring 2007Jan Plane (adapted from Bonnie Dorr)10Calling one method from another – static and non-staticnon-static methods when running they are associated with a specific instancedo have a “current object”are called with: objectName.methodName()static methodswhen running they are NOT associated with a specific instancedo NOT have a “current object”are called with:


View Full Document

UMD CMSC 131 - Lecture 10: Static Methods and Variables

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 10: Static Methods and Variables
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 10: Static Methods and Variables 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 10: Static Methods and Variables 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?