IBM i5/iSeries Primer(c) Concepts and Techniques for Programmers, Administrators, and Sys[... ]ators

Instantiation is the process of creating Java class objects. This process invokes a constructor method for the given class. Figure 26.13 shows the code for a slightly modified Vehicle class. This class accepts a name as a parameter and loads a variable with that name.

Figure 26.13: Java instantiation.

The application shown in Figure 26.14 illustrates the process of creating instances of the Vehicle class. The application creates three instances of the Vehicle class, and then prints data from each instance.

Figure 26.14: Java instantiation.

The following command invokes the Buycar application:

JAVA Buycar

The application creates three instances of the vehicle class and then prints an instance variable from each. The output is as follows:

Car 1 Color = Silver Car 2 Year = 2005 Car 3 Model = Mustang

Категории