Java Reference
In-Depth Information
Figure 2-1.
Did you notice that when the Enter key is hit, RAD automatically indents the insertion cursor and after a token is
entered, repositions the token correctly? This is done to make the code easier to understand (e.g., clearly show which
statements are in a method, which brace closes a method and the class, etc.) Although RAD tried to format the code
as it was entered, the code is not formatted as well as it could be. Badly formatted code can be difficult to read and
therefore easier to make mistakes. Fortunately, we can make RAD reformat all the source code and even specify the
formatting rules to follow.
On the menu bar, click Source and then Format .
12.
RAD will indent any nonindented or incorrectly indented class variable definitions and methods within the class.
In addition, any method statements are positioned as if they were indented twice. Notice that the statement on line
7 was moved to the left and the constructor's closing brace on line 8 was indented. RAD also added a blank line to
clearly separate the class variable definition from the constructor. Again, the purpose of the indentation and spacing
is to make it clear where methods begin and end and which statements are contained within a method.
13.
Enter the following statements after the constructor to define a method called
displayName:
public void displayName() {
}
This new method is intended to display the employee name that is associated with the variable empName.
Because empName is a class variable it does not have to be passed to the displayName method (remember class
variables can be accessed by any method within the class). displayName( ) is also defined as public (so other classes,
like EmployeeApp, can access it) and void was specified because nothing is returned.
14.
Add a blank line after the opening brace ( { ) of the displayName method and then enter
the following statement:
System.out.println(empName);
This statement displays the employee name by passing empName to the println method.
Click Source (on the Menu bar), and then Format to reformat the source code.
15.
The code should look like Figure 2-2 .
Search WWH ::




Custom Search