SECTION 8
Time Savers
Time Savers
Tip 61: Code More Quickly with Automatic Code Completion
The IDE’s code completion feature assists you with writing code as you type. Code completion
works in Java, Javadoc, HTML, XML, and Ant files, among other file types. Here are some examples
of how to use this time saver:
. Create a new Java class and add instance variables. Place the caret after the instance variables
and press Ctrl-Space to call up the code completion dialog. The IDE will suggest to generate a
matching constructor. The generated constructor will contain an appropriate method signa-
ture and skeleton code to initialize the instance variables (see Figure 63).
103
© 2009 Sun Microsystems. All rights reserved.
This publication is protected by copyright. Please see page 2 for more details.
100 NetBeans
IDE Tips & Tricks
by Ruth Kusterer
FIGURE 63:
Generating a
constructor for
existing fields
. Ctrl-Space additionally calls up a Javadoc
pop up for the selected method. If the
method is polymorphic or overloaded, it
will show all sets of parameters that this
method accepts. Press Enter on a list item
to complete the selected method parame-
ters automatically. Press the Tab key to
jump from one parameter to the next
(see Figure 64).
Tip
If Ctrl-Space is already assigned to another action in your locale, use Ctrl-Backslash instead.
SECTION 8
Time Savers
104
© 2009 Sun Microsystems. All rights reserved.
This publication is protected by copyright. Please see page 2 for more details.
100 NetBeans
IDE Tips & Tricks
by Ruth Kusterer
. If you prefer a more lightweight parameter pop up, invoke it by pressing Ctrl-P
(Mac: Command-P). This will quickly show the parameters this method expects
(see Figure 65).
. When you type for and then press Ctrl-Space, the IDE offers a list of common for loop
templates to choose from.
FIGURE 64:
Code completion
showing method
arguments and
Javadoc window
SECTION 8
Time Savers
The same also works with other keywords such as while and if. If you were actually looking for a
class starting with those letters (e.g., you typed
for, expecting java.util.Formatter), press Ctrl-Space
once more to show all possible completions.
105
© 2009 Sun Microsystems. All rights reserved.
This publication is protected by copyright. Please see page 2 for more details.
100 NetBeans
IDE Tips & Tricks
by Ruth Kusterer
FIGURE 65:
Lightweight
parameter
pop up
FIGURE 66:
Expanding code
templates for
for loops
SECTION 8
Time Savers
. When you type the first letters of a long camel case class name, and then press Ctrl-Space, the
IDE will also suggest classes matching these initials. You must type the initials in all capital
letters to trigger this handy feature.
For instance, type
ISR and press Ctrl-Space. The IDE suggests InputStreamReader and
InitialServerRequestDispatcher.
Tip Source
http://blogs.sun.com/roumen/entry/how_we_are_solving_some
http://www.antonioshome.net/blog/pivot/entry.php?id=12
Tip 62: Expand Templates and Abbreviations for
Common Code Snippets
Abbreviations save you a lot of time. Instead of typing the same common code snippets over and
over again, you type only a few letters and press the Tab key: The IDE automatically replaces the
abbreviation with the full code template.
Tip
Go to the Editor Options and set your preferred trigger key: Space, Tab, or Enter.
Here are a few examples for creating handy Java abbreviations.
. Typing psfs and pressing the Tab key completes to
private static final String
106
© 2009 Sun Microsystems. All rights reserved.
This publication is protected by copyright. Please see page 2 for more details.
100 NetBeans
IDE Tips & Tricks
by Ruth Kusterer
SECTION 8
Time Savers
. Typing sout and pressing the Tab key completes to
System.out.println(“|”);
. Declare a string—for example, name.
String name = “Duke”;
Now, typing soutv and pressing the Tab key expands to
System.out.println(“name = “ + name);
. Typing fori and pressing the Tab key completes to a for loop over an array. Instead of
imageNames, the IDE will automatically insert the last defined array in the same code block, if
there is one (see Figure 67).
107
© 2009 Sun Microsystems. All rights reserved.
This publication is protected by copyright. Please see page 2 for more details.
100 NetBeans
IDE Tips & Tricks
by Ruth Kusterer
FIGURE 67:
Generating a for
loop over a recently
defined array
Note that the counter variable i is highlighted with a blue background and is ready to be
edited. Press the Tab key to move the caret from one control expression to the next.

Get Checkered Flag Projects: 10 Rules for Creating and Managing Projects That Win! Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.