Previous | Table of Contents | Next |
14.3.1.4. How It Works
Whats going on here? How can a method that takes no arguments know what data to operate on? In fact, the area() method does have an argument! area() is implemented with an implicit argument that is not shown in the method declaration. The implicit argument is named this, and refers to this objectthe Circle object through which the method is invoked. this is often called the this pointer.16
16this pointer is C++ terminology. Since Java does not support pointers, I prefer the term this reference.
The implicit this argument is not shown in method signatures because it is usually not neededwhenever a Java method accesses the fields in its class, it is implied that it is accessing fields in the object referred to by the this argument. The same is true, as well see, when a method in a class invokes other methods in the classit is implicit that the methods are being invoked for the this object.
We can use the this keyword explicitly when we want to make explicit that a method is accessing its own variables and/or methods. For example, we could rewrite the area() method like this:
public double area() { return 3.14159 * this.r * this.r; }
In a method this simple, it is not necessary to be explicit. In more complicated cases, however, you may find that it increases the clarity of your code to use an explicit this where it is not strictly required.
An instance where the this keyword is required is when a method argument or a local variable in a method has the same name as one of the fields of the class. In this case, you must use this to access the field. If you used the field name alone, you would end up accessing the argument or local variable with the same name. Well see examples of this in the next section.
Take another look at how weve been creating our circle object:
Circle c = new Circle();
What are those parentheses doing there? They make it look like were calling a function! In fact, that is exactly what were doing. Every class in Java has at least one constructor method, which has the same name as the class. The purpose of a constructor is to perform any necessary initialization for the new object. Since we didnt define one for our Circle class, Java gave us a default constructor that takes no arguments and performs no special initialization.
The way it works is this: The new keyword creates a new dynamic instance of the classi.e., it allocates the new object. The constructor method is then called, passing the new object implicitly (a this reference, as we saw above), and passing the arguments specified between parentheses explicitly.
14.3.2.1. Defining a Constructor
There is some obvious initialization we could do for our circle objects, so lets define a constructor. Example 14.8 shows a constructor that lets us specify the initial values for the center and radius of our new Circle object. The example also shows a use of the this keyword, as described in the previous section.
EXAMPLE 14.8. A constructor for the Circle class.
public class Circle { public double x, y, r; // The center and the radius of the circle // The constructor method. public Circle(double x, double y, double r) { this.x = x; this.y = y; this.r = r; } public double circumference() { return 2 * 3.14159 * r; } public double area() { return 3.14159 * r*r; } }
With the old, default constructor, we had to write code like this:
Circle c = new Circle(); c.x = 1.414; c.y = -1.0; c.r = .25;
With this new constructor, the initialization becomes part of the object creation step:
Circle c = new Circle(1.414, -1.0, .25);
There are two important notes about naming and declaring constructors:
14.3.2.2. Multiple Constructors
Sometimes youll want to be able to initialize an object in a number of different ways, depending on what is most convenient in a particular circumstance. For example, we might want to be able to initialize the radius of a circle without initializing the center, or we might want to initialize a circle to have the same center and radius as another circle, or we might want to initialize all the fields to default values. Doing this is no problem: A class can have any number of constructor methods.
Example 14.9 shows how.
EXAMPLE 14.9. Multiple circle constructors.
public class Circle { public double x, y, r; public Circle(double x, double y, double r) { this.x = x; this.y = y; this.r = r; } public Circle(double r) { x = 0.0; y = 0.0; this.r = r; } public Circle(Circle c) { x = c.x; y = c.y; r = c.r; } public Circle() { x = 0.0; y = 0.0; r = 1.0; } public double circumference() { return 2 * 3.14159 * r; } public double area() { return 3.14159 * r*r; } }
Previous | Table of Contents | Next |