Java Reference
In-Depth Information
Tutorial: Passing Parameters and Formatting Source Code
Let's get started:
1.
Start RAD and in the Package Explorer pane, select myFirstPackage in MyFirstProject/src
by clicking on its name.
On the menu bar, click File , New , and then Class to display the “New Java Class” window.
2.
Notice that the source folder and package names are already specified. Because myFirstPackage was selected
in step 1, RAD assumed that was where the class should be created. A different project and package can be specified
by simply entering their names in the appropriate text fields.
3.
Specify Employee in the Name field.
Notice that the access modifier public has already been selected. Other modifiers can be chosen by clicking the
other modifier's option radio button or check boxes.
4.
Make sure none of the method stub options are selected.
Employee will be instantiated, not run as an application. Therefore, Employee does not need a main method.
5.
Click the Finish button to redisplay the Java perspective.
The Employee class should appear in the Navigation tree (along with the already existing EmployeeApp) and the
Employee source code generated by RAD will be displayed.
6.
Click on the blank line (line number 4) that follows the class header.
7.
The insertion cursor (a blinking vertical line) should appear at the far left of the line.
8.
Press the Enter key to insert a blank line.
9.
Define a String class variable by entering the following statement on the new line:
String empName = new String();
10.
Press Enter to insert another blank line.
11.
Define a constructor by entering the following 3 lines:
public Employee(String name) {
empName = name;
}
The Employee method is considered a constructor because it has the same name as the class and does not return
a value. The Employee constructor is defined as public (on line 6), meaning any class can access/use it. Notice that
Employee is expecting a String and will assign it to a String variable called name. On line 7, the value associated
with name will be assigned to empName (a class variable that was defined on line 5). The source code should look
like Figure 2-1 .
 
Search WWH ::




Custom Search