100 likes | 197 Views
Object Methods. Things To Remember. Private. Private variables may only be used by the class in which they are declared. No client program may ever access a private instance variable. Class Using Its Own P.I.V. public class Ball { private int diameter; private String color;
E N D
Object Methods Things To Remember
Private • Private variables may only be used by the class in which they are declared. • No client program may ever access a private instance variable.
Class Using Its Own P.I.V. • public class Ball { private int diameter; private String color; public void makeSameSize(Ball otherBall) { diameter = otherBall.diameter; } • This is a legal use of the P.I.V. since we are still in class Ball when we write .diameter • Note: there would also be constructors and other methods in this class.
Incorrect Use Of P.I.V. • public class BallVendor { public String sellBigBall(Ball myBall) { if(myBall.diameter > 10) return “Ball sold.” ; } } • This is an illegal use of a P.I.V. since this is not class Ball to which diameter belongs, but a separate class designed to use the Ball class a.k.a a client program.
this • Sometimes in the context of a class method we must refer to the object itself. • A classic example is the compareTo( ) method. • The convention for compareTo( ) is to return a negative if the object that called the method is less than or before the parameter object, a positive for the opposite, and a 0 if they are the same.
Using compareTo( ) • Let’s assume class BallVendor has: • Ball ball1 = new Ball(5); // diameter = 5 Ball ball2 = new Ball(6); // diameter = 6 int comp = ball1.compareTo(ball2); • comp should be a negative since ball1 is smaller
How It Works • The compareTo( ) method would then exist in class Ball • public intcompareTo(Ball that) { if(this.getDiameter( ) < that.getDiameter( ) ) return -1; else if(this.getDiameter( ) > that.getDiameter( ) ) return 1; else return 0; }
Constructors • It is the job of the constructor to assign a value to all private instance variables. • A default(empty) constructor gives the P.I.V. a default value: public Ball( ) { diameter = 0; } // 0 is good default for int } • All other constructors set the P.I.V. to the parameter: public Ball( int aDiameter) { diameter = aDiameter; } // P.I.V. = parameter
References v. Fundamental Type • MAJOR CAVEAT!! • If in a class: intx = 5; Ball b = new Ball(5); change(x,b); public void change(intone, Ball two) { one += 1; two.add1ToDiameter( ); } System.out.print(x + “ “ + b); • Assuming there were toString( ) and add1ToDiameter( ) methods in Ball, the result would print: 5 6 !!!!! • This is because one got x’s value, but when one changed, x did not. However two was told to point at the same object as b. When two called a method that changed the value of that object, b began to point to the changed object!
toString( ) • Every object class should have a toString( ) method. • JOPane and System.out automatically call this method on any variable. • This is why: int x = 10; System.out.print(x); would print the String version of the int 10 • Assuming Ball has a toString( ) that displays the diameter, Ball aBall = new Ball(10); System.out.print(aBall); • Would print 10.