Completing a Java Expression
See Also
As you are typing in the Source Editor, you can use the IDE's code completion
feature to find code snippets and have them filled in for you automatically. The code completion feature works by displaying a completion box that suggests possible
ways of completing the expression that you are typing. In addition,
a Javadoc box also
appears and shows the code documentation for the item currently selected in the
completion box.
You can use code completion to fill in:
- Names for packages, classes, methods, fields, and local variables
- try/catch blocks
- Import statements (when you use code completion to insert a class name without including its package name)
- Code from code templates
- Getter and setter methods for fields
- Skeletons for methods that the class inherits from superclasses or implemented interfaces
- Anonymous inner classes
To complete a Java expression:
-
In the Source Editor, press Ctrl-Spacebar or Ctrl-\ to open the code completion box.
The code completion box displays a scrolling list of
possible classes, methods, variables, and so on that can complete
your expression. If the IDE is aware of only one possible completion
for the expression, the expression is completed automatically.
You can open the code completion box either at the beginning of a line
or after you have already typed the beginning of an expression, such as
in one of the following examples:
- MyHashMap = new Hash (to select from class names beginning with Hash)
- MyHashMap = new HM (to select from names that have an uppercase H and an uppercase M, such as HashMap)
- MyHashMap = new java.util (to select from classes that are part of the java.util package)
- for (to select from code templates that use the for construct)
-
Use the most convenient combination of the following options to select an
item from the code completion box:
-
Continue typing to narrow down the selection of items in the list.
-
Use the mouse or navigation keys (up and down arrows, Page Up,
Page Down, Home, and End) to scroll through the list and select
an expression.
-
Press Enter to enter the selection in the completion box into
your file and close the code completion box.
Alternatively, you can
type a period (.), semicolon (;), or comma (,). If you type one of these characters,
the character is inserted after the generated completion text.
- Press Escape to close the code completion box without entering the selection.
Notes:
- The code
completion box opens automatically if you pause after typing a period or comma.
- If you press Enter for a method with parameters, the parentheses are added
and your insertion point is placed inside. A tooltip shows you the types that
you have to fill in for the parameters.
- Once you have entered a parameter, you can complete the statement by pressing
the semicolon (;) key. The semicolon is automatically placed at the end of the expression.
- Local variables are displayed in bold. The names of deprecated classes
and class members are struck through.
- You can display suggestions for parameters by pressing Alt-P.
- See Also
- Inserting Code From a Template
- Using Editor Hints to Fix Code
- Using the Word Match Feature
- Generating Getter and Setter Methods
- Managing Java Import Statements
- Code Completion Javadoc Box
- Setting the Completion Box Delay
- Disabling Automatic Completion
- Setting Code Completion Shortcuts
Legal Notices