, ,

Instance Members – Basics of Java Programming

1.4 Instance Members

The methods of an object define its behavior; such methods are called instance methods. It is important to note that these methods pertain to each object of the class. In contrast to the instance variables, the implementation of the methods is shared by all instances of the class. Instance variables and instance methods, which belong to objects, are collectively called instance members, to distinguish them from static members (p. 10), which only belong to the class.

Invoking Methods

Objects communicate by calling methods on each other. As a consequence, an object can be made to exhibit a particular behavior by calling the appropriate method on the object. This is achieved by a method call whose basic form is the following: a reference that refers to the object, the binary dot (.) operator, and the name of method to be invoked, together with a list of any arguments required by the method.

reference.methodName(listOfArguments)

The method invoked on the object can also return results back to its caller, via a single return value. The method called must be one that is defined for the object; otherwise, the compiler reports an error.

Click here to view code image

Point2D point = new Point2D(-1, -4);   // Creates a point with coordinates (-1,-4)
point.setX(-2);                        // (1) The x field is set to the value -2
int yCoord = point.getY();             // (2) Returns the value -4 of the y field
System.out.println(point.toString());  // (3) Prints: (-2,-4)
point.distanceFromOrigin();            // (4) Compile-time error: No such method.

The sample code above invokes methods on the object denoted by the reference point. The method call at (1) sets the value of the x field of point, and the method call at (2) returns the value of the y field of point. At (3), the call to the toString() method returns the string “(-2,-4)” which is printed. The setX(), getY(), and toString() methods are all defined in the class Point2D. The setX() method does not return any value, but the getY() and toString() methods do. Trying to invoke a method named distanceFromOrigin() at (4) on point results in a compile-time error, as no such method is defined in the class Point2D.

The dot (.) notation can also be used with a reference to access the fields of an object. The basic form for field access is as follows:

reference
.
fieldName

Use of the dot notation is governed by the accessibility of the member. The methods of the Point2D class are public and can thus be called by the clients of the class. However, the fields in the class Point2D have private access, indicating that they are not accessible from outside the class. Thus the code below at (1) in a client of the Point2D class will not compile. Typically, a class provides public methods to access values in its private fields, as class Point2D does.

Click here to view code image System.out.println(point.x);       // (1) Compile-time error: x is not accessible.
System.out.println(point.getX());  // OK.

Related Posts