1 / 15

Recitation 4

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

altessa
Download Presentation

Recitation 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recitation 4

  2. Classes public class Student { }

  3. Data Members public class Student { private String name; public String id; }

  4. 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

  5. Methods public class Student { String id; public void setID(String studentID){ id = studentID; } }

  6. Methods public void setID(String studentID) • Public or Private • Return type: void • Name method: setID • Argument: String studentID

  7. Methods Public class Student{ String id; public String getID(){ return id; } } • If the return type is not void, must have a return statement

  8. Class Constructor public class Student{ public String id; public Student(String studentID){ id = studentID; } }

  9. 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

  10. Using Student with the Constructor public class test{ public static void main(String[] args){ Student s; s = new Student(“12345”); } }

  11. 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());

  12. 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; } }

  13. 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”; } }

  14. 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

  15. 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

More Related