Java Methods


Java classes typically describe some real-world object, such as a customer, a purchase order, or a vehicle, as described above. These classes use instance variables, such as color, to define their properties. In addition to these properties or characteristics, most classes also have defined behaviors. These behaviors or actions are known as Methods. The Vehicle class shown in Figure 26.15 expands the previous version by adding the Paint method. This method changes the color of the instance.

image from book
Figure 26.15: Java methods.

Figure 26.16 shows the new Code for the Buycar application. This time, it creates a single instance of the Vehicle class, prints its original color, uses the Paint method to change the vehicles color, and then prints the new color.

image from book
Figure 26.16: Java methods.

The following command invokes the Buycar application:

      JAVA Buycar 

The output is as follows:

      Original Color = Silver      New Color = Gold 



IBM i5/iSeries Primer(c) Concepts and Techniques for Programmers, Administrators, and Sys[... ]ators
IBM i5/iSeries Primer(c) Concepts and Techniques for Programmers, Administrators, and Sys[... ]ators
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 245

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