Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects and Classes II Chapter 6, continued. Objects can be parameters Methods take parameters: e.g. Math.sqrt(3.0); takes 3.0 as a parameter Objects.

Similar presentations


Presentation on theme: "Objects and Classes II Chapter 6, continued. Objects can be parameters Methods take parameters: e.g. Math.sqrt(3.0); takes 3.0 as a parameter Objects."— Presentation transcript:

1 Objects and Classes II Chapter 6, continued

2 Objects can be parameters Methods take parameters: e.g. Math.sqrt(3.0); takes 3.0 as a parameter Objects can be used as parameters too: public static void printCircle(Circle c) { System.out.println("Circle area=" + c.findArea() + " "); } Circle c = new Circle(2.0); printCircle(c);

3 Case study: Example 6.5, p221 public class TestPassingObject { public static void main(String[] args) {....... Circle myCircle = new Circle(1.0); printAreas(myCircle, n); } public static void printAreas(Circle c, int times) {........ // repeatedly prints a circle and changes its radius } }

4 pass-by-value/pass-by-reference When a basic datatype (e.g. int) is passed as a parameter, a copy is made of it and the copy is used within the method: int i = 5; addOne(i); System.out.println("i is still " + i); This is different when we pass a reference to an object: a copy is made of the reference, but the object it refers to is still the same one!

5 pass-by-reference example Circle c = new Circle(2); doMonkeyBusiness(c); System.out.println("c's radius is " + c.radius); public static void doMonkeyBusiness(Circle circ) { circ.radius++; }

6 Previous example illustrated c: Circle radius=2 c main method doMonkeyBusiness circ radius=3

7 Reminder: basic datatypes i main method 2 addOne method num 2 3

8 A little privacy please..... doMonkeyBusiness altered the value of the radius directly. Usually, a class like Circle should not allow other code to alter its instance variables Instead, we make radius private, and provide public methods for accessing and changing the radius

9 public and private parts public class Circle { private double radius; // “private” means radius not accessible outside a Circle // object: myCircle.radius won’t work! public Circle() {.....} public Circle(double r) {.....} public double findArea() {......) public double getRadius() { return radius; } // since radius is private (myCircle.radius won’t work), // we have to use the getRadius method to get its value public void setRadius(double r) { radius = r; } // since radius is private (myCircle.radius=5 won’t work), // we have to use setRadius method to change its value }

10 getter and setter methods For each private instance variable x, we need to provide: –a getter method, which returns the value of x –a setter method, which sets the value of x Other code can access and alter x only by using these two methods We use methods, rather than allowing direct access to variables, because the methods let us check values are correct

11 Sample getter and setter private int width; // getter public int getWidth() { return width; } // setter public void setWidth(int newWidth) { width = newWidth; }

12 Use of getter and setter methods Circle c = new Circle(); c.setRadius(3.0); System.out.println("The radius of c is " + c.getRadius());

13 Summarizing Circle so far Circle private double radius; public Circle() public Circle(double r) public double getRadius() public void setRadius(double newRadius) public double findArea()

14 public and private modifiers Both methods and instance variables can be declared as either public or private If they are public, all parts of your code can use them if they are private, they can only be used within the declaring class, but not by other classes Instance variables are almost always private and are accessed (read or modified) through Getter and Setter methods

15 Static variables Each object of type Circle has its own radius....there is a separate copy of the variable radius for each object created. If instead we want all objects of a class to share data, we use a static variable Only one copy of a static variable is made, and all objects can access it static int numOfCircles; Each constructor can increment numOfCircles

16 Example: use of static variables public class Circle { private static int numOfCircles = 0; private double radius; Public Circle() { radius = 0.0; numOfCircles++; }...etc }

17 static constants Just like variables, we can declare a constant which is available for all members of a class. We do not need each object to have its own copy public final static double PI = 3.14159265358; Now other code can use this handy constant too: System.out.println("Pi is " + Circle.PI); Note that we use the classname to refer to a class constant

18 Class methods Some methods need to be associated with specific objects (e.g. findArea()) Others do not. These typically accept some value(s) as a parameter, and return some other value e.g. Math.sqrt(double d) public static double sqrt(double d) Class methods are associated with classes, and not with specific objects Class methods are therefore declared static: because we only want one of each class method. The main method is declared static because we only want one main method in our program.

19 Classes and Instances double d = Math.sqrt(3.0); // static Circle c = new Circle(3.0); d = c.findArea(); // not static d = c.getRadius(); // not static d = 2 * Circle.PI; // class constant d = MyInput.readDouble(); // static in general: Classname.Method(); // call to a static method Objectname.Method(); // call to a non-static method

20 When to use which? Constants are usually static Methods that do not read or modify instance variables are usually static Methods which use instance variables are not static Variables which describe common properties of an object can be made static Study Example 6.6 carefully (pp 225++)


Download ppt "Objects and Classes II Chapter 6, continued. Objects can be parameters Methods take parameters: e.g. Math.sqrt(3.0); takes 3.0 as a parameter Objects."

Similar presentations


Ads by Google