Objects and Their Methods


In addition to containing data, objects can also contain methods. You might have suspected as much, because all the application classes presented in this book have contained at least one method (public static void main(String[] args), and sometimes more than one. All those methods have had the keyword static in their declarations. Later in this chapter you will see that static means, in a sense, "not object-oriented." The methods had to be static because we had not yet introduced objects. Now it is time to present genuinely object- oriented methods.

Let's add a method to the Person class:

public class Person {   int    age;   short  weight;   int ageInNYears(int n)   {     return age + n;   } }

The method computes how old the person will be in n years. It has a lot in common with the methods you have already seen. It has a declaration that specifies the method's return type, name, and argument list. It has a body enclosed in curly brackets, and it returns a value.

There are two major differences between this method and the ones you have looked at in previous chapters:

  • There is no static in the declaration.

  • The method refers to a field (age) of the class where the method is defined.

To call a method of an object, you again use the "reference-dot" notation. The following code shows how this is done:

1. Person ed = new Person(); 2. ed.age = 62; 3. ed.weight = 220; 4. int n = ed.ageInNYears(3); 5. System.out.println("Ed will be " + n +                       " in 3 years.");

Note in line 4 that the method call looks like the method calls you are used to, except that it is preceded by an object reference and a dot. This syntax says, "Call the ageInNYears method of the object referenced by ed."

This brings up an important point about object-oriented programming. Until this chapter, all the methods presented in this book have contained in their declarations the mysterious keyword static. We will explain static in detail in the next section. For now, let's just say that a static method is one that is not object-oriented and does not belong to an individual object. It has been useful to present only static methods for six chapters, because the non-static approach allowed us to introduce a great many foundational concepts without the added complication of presenting objects. But in realistic Java programming, very few methods are static. Most methods are non-static, which means they are associated with objects. Thus, most method calls involve not just invoking a method, but invoking a method on an object.

Let's look again at the Person class, with line numbers:

 1. public class Person  2. {  3.   int    age;  4.   short  weight;  5.  6.   int ageInNYears(int n)  7.   {  8.     return age + n;  9.   } 10. }

The ageInNYears method makes use of the age variable. But which age variable? Every instance of the Person class has its own version of age (and of weight). You may have already guessed correctly: The version of age that gets used is the one belonging to the object on which the method call was made. So in the line int n = ed.ageInNYears(3);, the version of age that gets used is the one belonging to the object referenced by ed.

The ObjectMethodLab animated illustration demonstrates a class called Square. Start the program by typing java objects.SeveralObjectsLab. You will see the display shown in Figure 7.8.

click to expand
Figure 7.8: ObjectMethodLab

The class contains one variable, an int called side. The class also has one method, called dump, which outputs a message followed by a value. The output appears in the text area at the bottom of the window. Initially, the method dumps out side = , followed by side itself. Of course, this is the version of side that is owned by the object on which the method was called. The code creates two objects and gives them distinct values for side. These values are 10 and 20, but you can change them by typing different numbers into the text fields.

Try configuring the code so that the method dumps the perimeter of the square. Configure again so that the method dumps the area of the square. Observe how, when you call dump on an object, the method uses that object's version of side.

The next section will look deeper into how objects contain interacting data and methods.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net