150 likes | 270 Views
Recitation 4. Classes. public class Student { }. Data Members. public class Student { private String name; public String id; }. Data Members. private String variableName ; public String variableName ; Public means the variable can be accessed outside of its own class
E N D
Classes public class Student { }
Data Members public class Student { private String name; public String id; }
Data Members private String variableName; public String variableName; • Public means the variable can be accessed outside of its own class • Private means the variable is not accessible outside of its own class
Methods public class Student { String id; public void setID(String studentID){ id = studentID; } }
Methods public void setID(String studentID) • Public or Private • Return type: void • Name method: setID • Argument: String studentID
Methods Public class Student{ String id; public String getID(){ return id; } } • If the return type is not void, must have a return statement
Class Constructor public class Student{ public String id; public Student(String studentID){ id = studentID; } }
Class Constructor • Method defined within class • Not necessary to define one, a default one is created if no constructor is found • More than one constructor allowed, but each one must have a different set of arguments
Using Student with the Constructor public class test{ public static void main(String[] args){ Student s; s = new Student(“12345”); } }
Using Constructors and Methods • Must match up arguments for both constructor and method String id = “name”; Student s = new Student(id); System.out.println(s.getID());
Passing Variables • Primitive types are passed by value • Pass by value: passes a copy • Value of variable passed in won’t be changed by the method public class Test{ public static void main(String[] args){ i = 10; changevalue(i); System.out.println(i); //will print “10” } public void resetvalue(inti){ i = 0; } }
Passing Variables • Variables that hold references are passed by reference • Pass by reference: passes the reference of the object • Reference will be passed, so any changes made to the object inside the method affect the object passed in public class Test{ public static void main(String[] args){ Student s = new Student(“John Doe”); changeName(s); System.out.println(s.id); //will print “No Name” } public void clearName(Student s){ s.id = “No Name”; } }
Lifetime of Object Data Members • Each object name must be unique in its declaration space • Objects referenced by object data members, formal parameters, or local variables are only garbage collected if there are no other references to it • Object data members of an Object • Created when an object is created • Destroyed when an object is garbage collected • Formal Parameters • Created each time method is called • Destroyed when method finishes • Local Variables • Created on declaration • Destroyed at end of block
Extent of Objects • Name of data members can be reused by formal parameters or local variables (not both) • Keyword “this” refers to the object variable public class Student { private String id; public setID(String id) { this.id = id; } } • this.id refers to the id belonging to the object of type student • id refers to the Formal Parameter • This is used to simplify code: no need to invent different names