6 Appendix E Java Primer public Point(){ xCoord 0; yCoord =0; } Figure E.4 Default constructor for the Point class. contain one or more methods with the same name,as long as the data types in the parameter list are different. The Point class has two other methods:swap (and printIt ()Note that these methods are not declared as static;unlike the printIt()method in Figure E.2.These methods can be invoked with the statements ptA.swap(); ptA.printIt(); The difference between static and instance(nonstatic)methods is that static methods do not require being associated with an object,and we can call them by merely invoking the name of the method.Instance methods can be invoked only by being associated with an instance of an object.That is,they can be called with only the notation object-name.method.Also,an object containing the instance methods must be instantiated with new before its instance methods can be called.In this example,we must first create ptA to call the instance methods swap()and printIt(). Also notice that we declare the methods and data belonging to the class Point using either the public or private modifiers.A public declaration allows the method or data to be accessed from outside the class.Private declaration allows the data or methods to be accessed from only within the class. Typically,we design classes by declaring class data as private.Thus,the data for the class are accessible through only those methods that are defined within the class.Declaring data as private prevents the data from being manipulated from outside the class.In Figure E.3,only the swap()and printIt()methods can access the class data.These methods are defined as public,meaning that they can be called by code outside the class.Therefore,the designer of a class can determine how the class data are accessed and manipulated by restricting access to private class data through public methods.(In Section E.3,we look at the protected modifier.) Note that there are instances where data may be declared as public and methods as private.Data constants may be declared as public if access from outside the class is desired.We declare constants using the keywords static final.For example,a class consisting of mathematical functions may publicly declare the constant pi as public static final double PI =3.14159; Also,if a method is to be used only within the class,it is appropriate to define that method as private. Objects are treated by reference in Java.When the program creates an instance of an object,that instance is assigned a reference to the object.Figure
6 Appendix E Java Primer public Point() { xCoord = 0; yCoord = 0; } Figure E.4 Default constructor for the Point class. contain one or more methods with the same name, as long as the data types in the parameter list are different. The Point class has two other methods: swap() and printIt(). Note that these methods are not declared as static; unlike the printIt() method in Figure E.2. These methods can be invoked with the statements ptA.swap(); ptA.printIt(); The difference between static and instance (nonstatic) methods is that static methods do not require being associated with an object, and we can call them by merely invoking the name of the method. Instance methods can be invoked only by being associated with an instance of an object. That is, they can be called with only the notation object-name.method. Also, an object containing the instance methods must be instantiated with new before its instance methods can be called. In this example, we must first create ptA to call the instance methods swap() and printIt(). Also notice that we declare the methods and data belonging to the class Point using either the public or private modifiers. A public declaration allows the method or data to be accessed from outside the class. Private declaration allows the data or methods to be accessed from only within the class. Typically, we design classes by declaring class data as private. Thus, the data for the class are accessible through only those methods that are defined within the class. Declaring data as private prevents the data from being manipulated from outside the class. In Figure E.3, only the swap() and printIt() methods can access the class data. These methods are defined as public, meaning that they can be called by code outside the class. Therefore, the designer of a class can determine how the class data are accessed and manipulated by restricting access to private class data through public methods. (In Section E.3, we look at the protected modifier.) Note that there are instances where data may be declared as public and methods as private. Data constants may be declared as public if access from outside the class is desired. We declare constants using the keywords static final. For example, a class consisting of mathematical functions may publicly declare the constant pi as public static final double PI = 3.14159; Also, if a method is to be used only within the class, it is appropriate to define that method as private. Objects are treated by reference in Java. When the program creates an instance of an object, that instance is assigned a reference to the object. Figure
E.1 Basics 7 public class Third public static void main(String args[]){ /create 2 points and initialize them Point ptA new Point(5,10); Point ptB new Point (-15,-25); /output their values ptA.printIt(); ptB.printIt () /now exchange values for first point ptA.swap(); ptA.printIt(); /ptc is a reference to ptB Point ptC ptB; /exchange the values for ptC ptC.swap(); /output the values ptB.printIt(); ptC.printIt () } Figure E.5 Objects as references. E.5 shows two objects of class Point:ptA and ptB.These objects are unique; each has its own state.However,the statement Point ptc ptB; assigns ptC a reference to object ptB.In essence,both ptB and ptC now refer to the same object,as shown in Figure E.6.The statement ptc.swap(); alters the value of this object,calling the swap()method,which exchanges the values of xCoord and yCoord.Calling the printIt()method for ptB and ptc illustrates the change in the state for the object,showing that now the x coordinate is-25 and the y coordinate is-15. Whenever objects are passed as parameters to methods,they are passed by reference.Thus,the local parameter is a reference to the argument being passed.If the method alters the state of the local parameter,it also changes the state of the argument that it was passed.In Figure E.7,the object ptD is passed to the method change(),where the swap()method is called.When they return from change(),the values of the x and y parameters for ptD are exchanged. The Java keyword this provides an object a reference to itself.The this reference is useful when an object needs to pass a reference of itself to another object
E.1 Basics 7 public class Third { public static void main(String args[]) { // create 2 points and initialize them Point ptA = new Point(5,10); Point ptB = new Point(-15,-25); // output their values ptA.printIt(); ptB.printIt(); // now exchange values for first point ptA.swap(); ptA.printIt(); // ptC is a reference to ptB Point ptC = ptB; // exchange the values for ptC ptC.swap(); // output the values ptB.printIt(); ptC.printIt(); } } Figure E.5 Objects as references. E.5 shows two objects of class Point: ptA and ptB. These objects are unique; each has its own state. However, the statement Point ptC = ptB; assigns ptC a reference to object ptB. In essence, both ptB and ptC now refer to the same object, as shown in Figure E.6. The statement ptC.swap(); alters the value of this object, calling the swap() method, which exchanges the values of xCoord and yCoord. Calling the printIt() method for ptB and ptC illustrates the change in the state for the object, showing that now the x coordinate is −25 and the y coordinate is −15. Whenever objects are passed as parameters to methods, they are passed by reference. Thus, the local parameter is a reference to the argument being passed. If the method alters the state of the local parameter, it also changes the state of the argument that it was passed. In Figure E.7, the object ptD is passed to the method change(), where the swap() method is called. When they return from change(), the values of the x and y parameters for ptD are exchanged. The Java keyword this provides an object a reference to itself. The this reference is useful when an object needs to pass a reference of itself to another object