Presentation is loading. Please wait.

Presentation is loading. Please wait.

2.4 Creating and Using Objects. Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write.

Similar presentations

Presentation on theme: "2.4 Creating and Using Objects. Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write."— Presentation transcript:

1 2.4 Creating and Using Objects

2 Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write lines of code and small, complete programs that construct objects from system supplied classes and then make use of them.

3 Assuming that the Point class has been imported into a program, the following two lines of code 1) Declare a name which can be used for a Point object, and 2) Construct an object and give it that name, using the “=” sign, or assignment to do so: Point myPoint; myPoint = new Point(10, 20);

4 This is the specific syntax for calling a constructor and passing it parameters: new Point(10, 20); In order to call a constructor, the keyword “new” has to be used. The call shown will initialize the x and y coordinate values of the constructed point to 10 and 20, respectively. The two lines given above are frequently compressed into a single line of code: Point myPoint = new Point(10, 20);

5 When you construct an object and give it a name, you can think of this as putting a handle on the object. The name, “myPoint” is the handle. You can use this handle later on to refer to the object and manipulate it. To make this idea clearer, it is possible to consider the following example, where an object is created, but it is not given a name: new Point(10, 20);

6 The code is syntactically valid. It will not cause compile time or run time errors. It causes a Point object to be created. However, the object cannot be accessed in the rest of the program because it doesn’t have a name.

7 In the discussion above, the ideas were explained in terms of “giving an object a name”. The more technical term, which will be used in the rest of these notes, is “object reference”. A call to a constructor returns an unnamed reference to an object. When you declare a name, such as myPoint, and assign the unnamed reference to that name, the name becomes a reference to the object.

8 The idea of named and unnamed references can be illustrated further with another example. Consider the following line of code, which is syntactically correct. The println() method will accept the reference returned by a call to the constructor for a Point, even though the object is unnamed. This reference is a valid parameter and println() will print out information about the object referred to. System.out.println(new Point(10, 20));

9 Code where one call is contained in another is not particularly easy to read or understand, and the use of unnamed references is generally not very clear. This example is not given because it illustrates good code writing. It is given because it concretely illustrates that a call to a constructor causes an object to come into existence even if it remains unnamed.

10 If an object has been constructed and given a name, methods can be used on it. The Point class has a method translate(), which has the effect of shifting the location of a point in the plane by adding or subtracting values to its x and y coordinates. Here is an example of a line of code where a method is called on an object: myPoint.translate(30, 40);

11 The call takes this form: An object reference, a dot, a method name, and a parameter list. It is the dot which signifies the call of the method on the object. It is important to note the syntactic difference between calling a constructor and a method. The method call does not use the keyword “new”.

12 The effect of executing this line of code is to shift the point which was constructed above with x and y coordinates of 10 and 20 to a new location with an x coordinate of , or 40, and a y coordinate of , or 60. The line of code making the method call does not take the form of an assignment.

13 Values are assigned to instance variables as a result of the call, but the work of doing the assignments is hidden inside the method code. The Point class is a black box because it is not necessary to see the method code in order to correctly make use of the method.

14 Here is a synopsis of the basic method calling pattern, which will be repeated over and over again, with variations: object.method(parameters);

15 At this stage there is one more thing we can do with the object: Print it out. The printing methods will accept object references as parameters. If the object exists, this is a valid line of code: System.out.println(myPoint);

16 We are not yet doing graphical programming, and the output looks like this: java.awt.Point[x=10,y=20] The system is simply telling the type of the reference, namely, the class of the object, and what the current values of its instance variables are.

17 It is now possible to write an example program which includes all of the features described above: import java.awt.Point; /* This is the second example program. */ public class SecondProg { public static void main(String[] args) { Point myPoint = new Point(10, 20); System.out.println(myPoint); myPoint.translate(30, 40); System.out.println(myPoint); }

Download ppt "2.4 Creating and Using Objects. Writing the code for classes of your own will come later. At this time it is possible to understand and correctly write."

Similar presentations

Ads by Google