06EncapsulationIssuesSmall

06EncapsulationIssuesSmall - Lecture Set #6: Encapsulaton,...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) Lecture Set #6: Encapsulaton, “this”, junit testing and Libraries 1. Review of Parameter passing 2. this 3. public vs. private Choices 4. junit testing 5. Libraries CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 1 Tracing Parameters and Methods c Recall that methods / constructors can have parameters public static int funnyAdd(int first, int second) { int localVal = 8; localVal += first++ + ++second; System.out.println(first+” & “+second); return localVal; } c What is printed by the following? pubic static void main(String[] args){ int x = 3, y = 2; int sum = funnyAdd(x,y); System.out.println(“sum = “, sum); int sum = funnyAdd(x-y,y+1); System.out.println(“sum = “, sum); } CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 2 Parameters and Constructors c Recall that methods / constructors can have parameters public Student (String newName, int IDDesired) { name = newName; id = IDDesired; tokenLevel = 3; } c What is printed by the following? String newName = “Joe”; Student s = new Student(newName + “ Schmoe”, 123456789); System.out.println (s.name); System.out.println (newName); c Joe Schmoe Joe
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 3 How Does Java Evaluate Method / Constructor Calls? int newName = “Joe”; Student s = new Student (newName + “ Schmoe”, 123456789); 1. Arguments are evaluated using stack in effect at call site (place where method called) c newName + “ Schmoe”, evaluates to Joe Schmoe c 123456789 evaluates to 123456789 2. Stack frame (temporary addition to stack) created to associate method parameters with values 3. Stack frame put into stack 4. Body of method executed in modified stack 5. Stack frame removed from stack CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 4 this c a reference to the current object. (Only makes sense in a non-static method.) c In an instance method, this is the object that is assumed c easy to refer to members (data or methods) using the assumed object c difficult to refer to the whole object without having a name to call it c Only use when needed – using it all the time makes the code more difficult to read CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dor ) 5 Public Declarations c public variables/methods and classes c Keyword public used in declaration c Every user of an object can access any public element c Sometimes access should be restricted! c
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

06EncapsulationIssuesSmall - Lecture Set #6: Encapsulaton,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online