![]() |
![]() |
For the most part, as you have progressed through this Getting Started document, you have been using the Visual Composition Editor and the SmartGuides to generate Java code for you, or you have been copying in sections of code that we have provided for you. When you create your own applications, you will likely need to write sections of code by hand, in the Source panes of the IDE browsers. VisualAge for Java provides several tools to help you write correct, neat code by hand. This section describes some of these tools.
Source panes and some other dialogs and browsers (for example, the Configure Breakpoints dialog) contain code assist, a tool to help you find the classes, methods, and fields you are looking for without having to refer to class library reference information. Code assist is accessed by typing Ctrl+Spacebar.
When you type Ctrl+Spacebar, classes methods, and types that could be inserted in the code at the cursor are shown in a pop-up list, from which you can select one. Code assist performs a visibility check and classes, methods, and fields that are not visible are not displayed. If the code assist mechanism cannot find a member that fits the current location of the cursor, the information line at the bottom of the pane will indicate that no code assist is available for the current context.
Code Assist for Types
To insert the name of a class or interface in your code, enter the first one or
more letters of the type name, and then type Ctrl+Spacebar. A pop-up list appears,
containing types that start with what you have entered. Enter more letters to narrow down
the list. Select an item to insert it into your code at the cursor. If the type needs to
be qualified, the qualification is also automatically inserted.
Example:
Create a test project and package. In the test package, create a class called
AssistTest. In the AssistTest class, create a method called assistMethod. Suppose you want
to declare a local Integer variable, i. In the body of the assistMethod
source, type the following letters:
In
Type Ctrl+Spacebar. The pop-up list of options will appear.
The list of available types is long. To find "Integer," enter the letters "te". Now, "Integer" will be near the top of the list.
Select it using the arrow keys and Enter.
Now, finish the declaration, so that the method looks like this:
public void assistMethod() { Integer i; }
Save the method by typing Ctrl+S. You will use this test method in the next example.
Code Assist for Methods and Fields
Code assist will also list the methods and fields available for an object or
class. Enter objectName., and optionally one or more letters from the
start of the method or field name, and then type Ctrl+Spacebar. The list of methods and
fields for the object will pop-up. Select one to insert it in the code.
Example:
In the assistMethod method you created in the previous example, below the line
that declares i, enter the following code (the period is important):
i = Integer.
Type Ctrl+Spacebar. A list of methods and fields in Integer will pop up.
Enter the letters "val", until you find "valueOf(String) Integer". The parameter types (in this case "String") and return type ("Integer") are shown.
Select "valueOf(String) Integer", and it will be inserted into your code. Enter a string such as "35" between the parentheses and end the line with a semi-colon. The method source will now look like this:
public void assistMethod() { Integer i; i = Integer.valueOf("35"); }
If you request code assist for a method or field from a class that requires qualification, the class must be qualified before you type Ctrl+Spacebar. Otherwise, no code assist will be available. Generally, the code that appears before the cursor must be compilable before you request code assist.
Example:
Suppose java.util.* is not in your class' import statement. This means that the class
ResourceBundle must be qualified when you use it in your class. If you type the following
code, and then type Ctrl+Spacebar to get the list of methods available, no list will be
available:
public String newMethod ( ) { ResourceBundle a = ResourceBundle. // place cursor after period // and type Ctrl+Spacebar
However, if you type the following code, where the class qualification is provided, code assist is available:
public String newMethod ( ) { ResourceBundle a = java.util.ResourceBundle. // place cursor after period // and type Ctrl+Spacebar
An easier way to produce a qualified name in this case (assuming you do not want to add this class or package to the import list) is to place the cursor before the period and type Ctrl+Spacebar. Select the class name from the list and it will be fully qualified for you automatically. Then type the period and Ctrl+Spacebar. The list of methods in ResourceBundle will now pop-up.
Code Assist for Method Parameters
Code assist includes pop-up help for method parameters. For example when you select
"valueOf(String) Integer" from the pop-up list in an example, above, the
following text is inserted at the cursor:
valueOf()
The cursor is automatically placed between the parentheses, and the pop-up label "String" appears to let you know what type of parameter to add.
Important to Note:
If you try to save code that contains an error, the IDE warns you that the code has an error. If it can determine the type of error, it will present a list of possible solutions. You can select one and correct the error, or you can save the code with the error (it will be added to the list of problems on the Problem page of the IDE browsers that contain the program element).
For example, add the following line (including the mistake) to the assistMethod method from above:
System.out.pritn(i);
When you save the method, the following dialog will appear, suggesting alternative code that will fix the problem:
Select the suggested correction "print(Object) void" and click Correct. The method will be saved with the replacement code. If you click Save, the method will be saved with the error. If you click Cancel, the method will not be saved and the error will remain in the code.
To promote neat, easy-to-read coding, the IDE provides an automatic code formatter which automatically controls how your code appears when you write it in a Source pane. To set code formatter options, including indentation and new-line controls:
These specifications are applied automatically to all new code. If you have imported code from the file system, or if you change the formatting options, you can apply the options to code in a particular source pane by selecting Format Code from the pane's pop-up menu.
![]() |
![]() |