Java Reference
In-Depth Information
Figure 2-2.
Save the source code by clicking
File
and then
Save
.
16.
Tutorial: Defining a Java Application
We need to change EmployeeApp to create (instantiate) an Employee object and then invoke the Employee object's
displayName method. Figure
2-3
shows the relationship between the user, EmployeeApp, and the Employee object.
Figure 2-3.
1.
In the Package Explorer pane, double click EmployeeApp to display the Java source code.
Just as EmployeeApp currently creates (instantiates) a
String
object, we will change it to instantiate an
Employee object.
2.
Replace the statement that creates sample with the following:
Employee emp =
new
Employee(
new
String("Joe Employee"));
Notice that the syntax for creating the Employee object and assigning it to a variable is the same as creating a
String
object. First you specify the variable type (the class name Employee) followed by at least one space and the name of
the variable (emp). emp is assigned (=) a new Employee object (
new Employee
). Finally, the new Employee object's
constructor is passed the
String
object it is expecting (in this case, a
String
object with the text “Joe Employee”).
Let's go into a little more detail about what exactly is happening between the Employee and EmployeeApp classes.