Content assist | Content assist provides you
with a list of suggested completions for partially entered strings.
In the Java editor press Ctrl+Space or invoke Edit >
Content Assist.
|
Content assist in Javadoc comments | Content assist is also
available in Javadoc comments.
|
Suppress types in code
assist![]() |
To exclude certain types from appearing in content assist, use the type filter feature configured on the Java > Type Filters preference page. Types matching one of these filter patterns will not appear in the Open Type dialog and will not be available to code assist, quick fix and organize imports. These filter patterns do not affect the Package Explorer and Type Hierarchy views. ![]() |
Content assist for variable, method parameter and field name completions |
You can use content assist to speed up the creation of fields, method parameters and local variables. With the cursor positioned after the type name of the declaration, invoke Edit > Content Assist or press Ctrl+Space.
If you use a name prefix or suffix for fields, local variables or method parameters, be sure to specify this in the Code Style preference page (Window > Preferences > Java > Code Style). |
Parameter Hints | With the cursor in a method
argument, you can see a list of parameter hints. In the Java Editor
press Ctrl+Shift+Space or invoke Edit > Parameter Hints.
|
Content assist on anonymous classes | Content assist also
provides help when creating an anonymous class. With the cursor
positioned after the opening bracket of a class instance creation,
invoke Edit > Content Assist or press Ctrl+Space.
|
Toggle between inserting and replacing code assist | When code assist is invoked
on an existing identifier, code assist can either replace the
identifier with the chosen completion or do an insert. The
default behavior (overwrite or insert) is defined in Window >
Preferences > Java > Editor > Code Assist. You can temporarily toggle the behavior while inside the content assist selection dialog by pressing and holding the Ctrl key while selecting the completion. |
Incremental content assist | Check Insert common prefixes automatically on the Java > Editor > Syntax preference tab to use shell-style incremental completion in the Java editor. If the available completions do not have a common prefix, the proposal popup is displayed. |
Create Getter and Setters dialog | To create getter and setter
methods for a field, select the field's declaration and invoke Source
> Generate Getter and Setter.
|
Use content assist to
create Getter and Setters![]() |
Another way to create
getters and setters is using content assist. Set the cursor in the
type body between members and press Ctrl+Space to get the proposals
that create a getter or setter method stub. ![]() |
Delete Getters and Setters together with a field | When you delete a field from within a view, Eclipse can propose deleting its Getter and Setter methods. If you use a name prefix or suffix for fields, be sure to specify this in the Code Style preference page (Window > Preferences > Java > Code Style). |
Create delegate methods | To create a delegate method
for a field select the field's declaration and invoke Source >
Generate Delegate Methods. This adds the selected methods
to the type that contains a forward call to delegated methods. This
is an example of a delegate method: ![]() |
Use Drag & Drop for refactoring | You can move Java compilation units between packages by Drag & Drop - all missing imports will be added and references updated. |
Use Drag & Drop to move and copy Java code elements | You can move and copy Java elements such as methods and fields by Drag & Drop. This will not trigger refactoring - only the code will be copied or moved. |
Use Templates to create a method | You can define a new
template (Preferences > Java > Editor > Templates)
that contains a method stub. Templates are shown together with the Content
Assist (Ctrl+Space) proposals. There are also existing templates, such as 'private_method', 'public_method', 'protected_method' and more. Use the Tab key to navigate between the values to enter (return type, name and arguments).
|
Use Quick Fix to create a new method | Start with the method
invocation and use Quick Fix (Ctrl+1) to create the method.
|
Use Quick Fix to change
a method signature![]() |
Add an argument to a method
invocation at a call site. Then use Quick Fix (Ctrl+1) to add the
required parameter in the method declaration.
|
Use Content Assist to
create a constructor stub![]() |
At the location where you
want to add the new constructor, use code assist after typing the
first letters of the constructor name.![]() |
Create new fields from
parameters![]() |
Do you need to create new
fields to store the arguments passed in the constructor? Use quick
assist (Ctrl + 1) on a parameter to create the assignment and the
field declation and let Eclipse propose a name according to your Code
Style preferences.![]() |
Use Content Assist to override a method | Invoke Content Assist
(Ctrl+Space) in the type body at the location where the method
should be added. Content assist will offer all methods that can be
overridden. A method body for the chosen method will be created.
|
Use Quick Fix to add unimplemented methods | To implement a new
interface, add the 'implements' declaration first to the type.
Even without saving or building, the Java editor will underline the
type to signal that methods are missing and will show the Quick Fix
light bulb. Click on the light bulb or press Ctrl+1 (Edit >
Quick Fix) to choose between adding the unimplemented methods or
making your class abstract.
|
Override a method from a base class | To create a method that
overrides a method from a base class: Select the type where the methods should be added and invoke Source > Override / Implement Methods. This opens a dialog that lets you choose which methods to override.
|
Rename in File | To quickly do a rename that
doesn't require full analysis of dependencies in other files, use the
'rename in file' Quick Assist. In the Java Editor, position the
cursor in an identifier of a variable, method or type and press Ctrl+1
(Edit > Quick Fix) The editor is switched to the linked edit mode (like templates) and changing the identifier simultaneously changes all other references to that variable, method or type.
|
Use Quick Fix to handle exceptions | Dealing with thrown exceptions is
easy. Unhandled exceptions are detected while typing and marked with
a red line in the editor.
|
Less typing for assignments | Instead of typing an assignment, start
with the expression that will be assigned.
![]() |
Less work with cast
expressions![]() |
Don't spend too much time with typing casts. Ignore them first and use quick assist to add them after
finishing the statement. For example on assignments: ![]() Or in for method arguments: ![]() Or for method call targets ![]() |
Surround lines | To surround statements with an if /
while / for statement or a block, select the lines to surround and
press Ctrl+1 (Edit > Quick Fix). This lists all templates
that contain the variable ${line_selection}.
|
Create your own templates |
To create your own templates, go to the Java > Editor > Templates preference page and press the New button to create a template. For example, a template to iterate backwards in an array would look like this: for (int ${index} = ${array}.length - 1; ${index} >= 0; ${index}--){ ${cursor} } |
Code assist can insert argument names automatically |
You can have code assist insert argument names automatically on method completion. This behavior can be customized on the Java > Editor > Code Assist preference page (see the Fill argument names on method completion checkbox.) For example, when you select the second entry here,
code assist will automatically insert argument names:
you can then use the Tab key to navigate between the inserted names. Code assist can also guess argument names - based on their declared types. This can be configured by the Guess filled argument names checkbox on the Java > Editor > Code Assist preference page. |
Remove surrounding statement | To remove a surrounding statement or
block, position the cursor at the opening bracket and press Ctrl+1
(Edit > Quick Fix).
|
How was that word spelled again?![]() |
You can enable spell-checking support in the Java editor on the
Java > Editor > Spelling preference page. Spelling errors
are displayed in the Java editor and corresponding Quick Fixes are available:
You can make the dictionary also available to the content assist. However, there is currently no dictionary included in Eclipse. The required format is just a list of words separated by newlines and the Quick Fixes allow you to add new words to the dictionary on-the-fly. Contributions of dictionaries would be welcome. |
Structured selections | You can quickly select Java code
syntactically using the Structured Selection feature. Highlight the text and press Alt+Shift+Arrow Up or select Edit > Expands Selection To > Enclosing Element from the menu bar - the selection will be expanded to the smallest Java-syntax element that contains the selection. You can then further expand the selection by invoking the action again. |
Find the matching bracket | To find a matching bracket select an
opening or closing bracket and press Ctrl+Shift+P (Navigate >
Go To > Matching Bracket). You can also double click
before an opening or after a closing bracket - this selects the text
between the two brackets.
|
Smart Javadoc | Type '/**' and press Enter. This
automatically adds a Javadoc comment stub containing the standard
@param, @return and @exception tags.
|
Use the local history to revert back to a previous edition of a method | Whenever you edit a file,
its previous contents are kept in the local history. Java tooling
makes the local history available for Java elements, so you can
revert back to a previous edition of a single method instead of the
full file.
Select an element and use Replace With > Local History to revert back to a previous edition of the element.
|
Use the local history to restore removed methods | Whenever you edit a file,
its previous contents are kept in the local history. Java tooling
makes the local history available for Java elements, so you can
restore deleted methods selectively.
Select a container and use Restore from Local History to restore any removed members.
|
Customizable code generation | The Java > Code Style
> Code Templates preference page allows you to customize
generated code and comments in a similar way to normal templates.
These code templates are used whenever code is generated. (These new
templates replace the 'filecomment' and 'typecomment' templates
previously used for code generation in R2.1.)
|
Sort members | You can Sort Members
of a Java compilation unit according to a category order defined in
the Java > Appearance > Members Sort Order preference
page. You'll find the action under Source > Sort Members |
Wrap Strings |
You can have String literals wrapped when you edit them. For example, if you have code like this: String message= "This is a very long message."; position your caret after the word "very" and press Enter. The code will be automatically changed to: String message= "This is a very" + " long message."; This behavior can be customized in the Java > Editor > Typing preference page. |
Smart Typing and how to control it![]() |
The Java editor's Smart Typing features ease your daily work. You can configure
them on the Typing tab of the Java > Editor preference page.
Note that the Smart semicolon positioning and Smart brace positioning are disabled by default. When Close brackets and parenthesis is enabled, you usually have to press an additional key to position the cursor behind the automatically inserted bracket or parenthesis before entering an opening brace or semicolon. Smart semicolon positioning and Smart brace positioning automatically position the semicolon or brace behind the inserted bracket or parenthesis. You can undo this automatic positioning by pressing backspace right afterwards. |
Fix your code
indentation with one key stroke![]() |
A useful feature is Source > Correct Indentation. Select the code where the indents are incorrect and invoke the action. |
Quick menus for source and
refactoring actions![]() |
The refactoring and source actions can be accessed via a quick menu. Select the element to be manipulated in the Java editor or in a Java view and press Alt+Shift+S for the quick source menu or Alt+Shift+T for the quick refactor menu. ![]() |
Find unused code | The Java compiler detects
unreachable code, unused variables, parameters, imports and unused
private types, methods and fields.
The setting is on the Java > Compiler preference page.
|
Javadoc comment handling![]() |
The Eclipse Java compiler can process Javadoc comments. Search
reports references in doc comments, and refactoring updates these
references as well. This feature is controlled from the Java >
Compiler > Javadoc preference tab (or set for an individual
project using Project > Properties > Java Compiler >
Javadoc).
|
Locate variables and their read/write access | You can locate variables
and see their read/write status by selecting an identifier
(variable, method or type reference or declaration) and invoking Search
> Occurrences in File. This marks all references of this
identifier in the same file. The results are also shown in the search
view, along with icons showing the variable's read or write access.
Alternatively, use the new Mark Occurrences feature to dynamically highlight occurrences. You can search over several files by using the general search features (Search > References). |
Search for methods with a specific return type |
To search for methods with a specific return type, use "* <return type>" as follows:
|
Remove Javadoc results from Java search | By default Java Search finds references inside Java code and Javadoc. If you don't want to find references inside Javadoc, you can disable this behavior by unchecking Process Javadoc comments of Java > Compiler > Javadoc preference page. |
Trace method call chains
with the Call Hierarchy![]() |
Have you ever found yourself searching for references to methods again and again? Use the new Call Hierarchy to follow long or complex call chains without losing the original context: Just select a method and invoke Navigate > Open Call Hierarchy (Ctrl+Alt+H).
|
Declaration view![]() |
There is a new Declaration view (Window > Show View >
Other > Java > Declaration) which shows the source of the
element selected in the Java editor or in a Java view.
|
||
Javadoc view![]() |
There is a Javadoc view (Window > Show View > Other
> Java > Javadoc) which shows the Javadoc of the element
selected in the Java editor or in a Java view. The Javadoc view uses
the SWT Browser widget to display HTML on platforms which support it.
|
||
Type hierarchy view supports grouping by defining type | The type hierarchy method
view lets you sort the selected type's methods by its defining types.
For example, for AbstractList you can see that it contains methods
that were defined in Object, Collection and List:
|
||
Tricks in the type hierarchy |
|
||
Find out where a method is implemented in the hierarchy | To find out
which types in a hierarchy override a method, use the 'Show Members in
Hierarchy' feature.
|
||
Hierarchical vs. flat layout of packages | An option on the Java
Packages view (and Package Explorer view) allows you to change the
way packages are displayed. Hierarchical displays packages in
a tree, with sub-packages below packages; Flat displays them
in the standard arrangement, as a flat list with all packages and
sub-packages as siblings.
|
||
Logical packages | The Java Packages view
(Java Browsing perspective) coalesces packages of the same name
across source folders within a project. This shows the Packages view
containing a logical package.
|
||
Compress package names | If your package names are
very long you can configure a compressed name that appears in the viewers.
Configuration of the compression pattern is done in Preferences
> Java > Appearance
|
JUnit | Select a JUnit test method in a view and choose Run > JUnit Test from the context menu or Run > Run As > JUnit Test from the main menu. This creates a launch configuration to run the selected test. |
Hide JUnit view until errors or failures occur | You can make the JUnit view
open only when there are errors or failures. That way, you can have
the view set as a fast view and never look at it when there are no
failing tests. While there are no problems in your tests you will see
this icon ![]() ![]() ![]() ![]() |
Content assist in dialog fields | Content Assist (Ctrl+Space) is now also available in input fields
of various Java dialogs. Look for small light bulb icon beside the
field when it has focus.
Content Assist is e.g. implemented in the New Java Class, New Java Interface, and New JUnit Test wizards, as well as in the refactoring dialogs for Change Method Signature and moving static members. The Extract Local Variable, Convert Local Variable to Field, and Introduce Parameter refactorings offer content assist proposals for the new element name. |
Structural compare of Java source | A structural comparison of Java source
ignores the textual order of Java elements like methods and fields
and shows more clearly which elements were changed, added, or removed. For initiating a structural comparison of Java files you have two options:
|
Structural compare of property files | A structural comparison of Java
property files (extension: .properties) ignores the textual order of
properties and shows which properties were changed, added, or
removed. For initiating a structural comparison of property files you have two options:
|
Define prefixes or suffixes for fields, parameters and local variables | In addition to configuring
the prefix or suffix for fields, you can also specify the prefix or
suffix for static fields, parameters, and local variables. These
settings on the Java > Code Style preference page are used
in content assist, quick fix, and refactoring whenever a variable
name is computed.
|
Organize Imports works on more than single files | You can invoke Organize Imports on sets of compilation units, packages, source folders or Java projects. |
Format more than one
file![]() |
Select all Java files to format and choose Source > Format to format them all. The format action is also available on packages, source folders or Java projects. |
Use project specific compiler settings | Each project can
use the global compiler settings or you can define project specific
settings. Select the project and open the Java compiler page in the
project properties (Project > Properties > Java Compiler)
|
Use a specific JRE for a project | When creating new projects the JRE that
is added by default is the one selected in Preferences > Java
> Installed JRE's. To set a project specific JRE, open the
project's Java Build path property page (Project > Properties
> Java Build Path), then the Libraries page, select
'JRE System Library' and press Edit. In the 'Edit Library'
dialog you can select either the default JRE or a project specific
JRE to add to new projects.
|
Propagating deprecation tag |
The Java compiler can be configured to diagnose deprecation using
options on the Java > Compiler > Advanced page.
|
Recovering from abnormal inconsistencies |
In the rare event of a dysfunction, JDT could reveal some inconsistencies
such as:
To make it consistent again, the following actions need to be performed in this exact order:
|
Launching from the Context Menu |
Launching is now available from the context menu of many items. You can launch from a source file, package, method, type, etc. by choosing Run (or Debug) > Java Application from the context menu.
|
Evaluations in the debugger |
Snippet evaluation is available from a number of places in the debugger. Choosing Display or Inspect from the context menu of the editor or Variables view will show the result in a pop-up whose result can be sent to the Display or Expressions views.
|
View Management in Non-Debug perspectives |
The Debug view automatically manages debug related views based on the view selection (displaying Java views for Java stack frames and C views for C stack frames, for example). By default, this automatic view management only occurs in the Debug perspective, but you can enable it for other perspectives via the View Management preference page available from the Debug view toolbar pulldown. |
Environment Variables |
You can now specify the environment used to launch Java applications via the Environment tab.
|
String Substitutions |
Variables are now supported for many parameters of Java Application launch configurations. Most fields that support variables have a Variables... button next to them. The Main Type field supports variables as well; the ${java_type_name} variable allows you to create a configuration that will run the selected type. |
Logical Structures |
The Logical Structures toggle on the Variables view toolbar presents alternate structures for common types. JDT provides logical views for Maps, Collections, and SWT Composites.
|
Default VM Arguments |
If you specify the same arguments to a certain VM frequently, you can configure Default VM Arguments in the Installed JREs preference page. This is more convenient than specifying them for each launch configuration.
|
Stop in Main |
You can use Stop in main in a Java Application launch configuration to cause your program to stop at the first executable line of the main method when you run it under debug mode.
|
Conditional breakpoints | You can use conditional breakpoints in
Breakpoint Properties... to control when a breakpoint
actually halts execution. You can specify whether you want the
breakpoint to suspend execution only when the condition is true, or
when the condition value changes.
|
Disabling breakpoints |
If you find yourself frequently adding and removing a breakpoint in the same place, consider disabling the breakpoint when you don't need it and enabling it when needed again. This can be done using Disable Breakpoint in the breakpoint context menu or by unchecking the breakpoint in the Breakpoints view. You can also temporarily disable all breakpoints using the Skip All Breakpoints action in the Breakpoints view toolbar. This will tell the debugger to skip all breakpoints while maintaining their current enabled state.
|
Changing variable values |
When a thread is suspended in the debugger, you can change the values of Java primitives and Strings in the Variables view. From the variable's context menu, choose Change Variable Value. You can also change the value by typing a new value into the Details pane and using the Assign Value action in the context menu (CTRL-S key binding).
|
Variable values in hover help | When a thread is suspended and the
cursor is placed over a variable in the Java editor, the value of
that variable is displayed as hover help.
|
Drop to Frame |
When stepping through your code, you might occasionally step too far, or step over a line you meant to step into. Rather than restarting your debug session, you can use the Drop to Frame action to quickly go back to the beginning of a method. Select the stack frame corresponding to the Java method you wish to restart, and select Drop to Frame from Debug view toolbar or the stack frame's context menu. The current instruction pointer will be reset to the first executable statement in the method. This works for non-top stack frames as well. Note that Drop to frame is only available when debugging with a 1.4 or higher VM, or the J9 VM. There are some situations where a JVM may be unable to pop the desired frames from the stack. For example, it is generally impossible to drop to the bottom frame of the stack or to any frame below a native method. |
Hot code replace | The debugger supports Hot Code Replace when debugging with a 1.4 or higher VM, or the J9 VM. This lets you make changes to code you are currently debugging. Note that some changes such as new or deleted methods, class variables or inner classes cannot be hot swapped, depending on the support provided by a particular VM. |
Stepping into selections |
The Java debugger allows you to step into a single method within a series of chained or nested method calls. Simply highlight the method you wish to step into and select Step into Selection from the Java editor context menu. This feature works in places other than the currently executing line. Try debugging to a breakpoint and stepping into a method a few lines below the currently instruction pointer. |
Controlling your console |
Output displayed in the console can be locked
to a specific process via the Pin Console action in the
Console view toolbar. There's also a Scroll Lock action
that stops the console from automatically scrolling as new output is appended.
|
Creating watch items | A watch item is an expression in the Expressions view whose value is updated as you debug. You can create watch items from the Java editor by selecting an expression or variable and choosing Watch from its context menu or the top-level Run menu. |
Watch points |
A watch point is a breakpoint that suspends execution whenever a specified variable is accessed or modified. To set a watchpoint, select a variable in the Outline view and choose Toggle Watchpoint from its context menu. To configure a watchpoint, select the watchpoint in the Breakpoints view and choose Properties... from its context menu. The most important properties for this type of breakpoint are the Access and Modification checkboxes which control when the breakpoint can suspend execution.
|
Threads and Monitors view |
The debugger's Threads and Monitors view shows which threads are holding locks and which are waiting to acquire locks. |
Step filters | Step filters prevent the debugger from suspending in specified classes and packages when stepping into code. Step filters are established in Window > Preferences > Java > Debug > Step Filtering. When the Use Step Filters togggle (on the debug toolbar and menu) is on, step filters are applied to all step actions. In the Debug view, the selected stack frame's package or declaring type can be quickly added to the list of filters by selecting Filter Type or Filter Package from the stack frame's context menu. |
Using the scrapbook |
If you want to experiment with API or test out a new algorithm, it's frequently easier to use a Java scrapbook page than create a new class. A scrapbook page is a container for random snippets of code that you can execute at any time without a context. To create a scrapbook page, create a new file with a .jpage extension (or use the New wizard - Java > Java Run/Debug > Scrapbook Page). Enter whatever code you wish to execute, then select it. There are three ways to execute your code:
These actions are in the workbench toolbar and also in the scrapbook page's context menu. |
Editing launch configurations |
Holding down the Ctrl key and making a selection from the Run or Debug drop-down menu opens the associated launch configuration for editing. The launch configuration can also be opened from the context menu associated with any item in the Debug view. |
Favorite launch configurations |
Launch configurations appear in the Run/Debug drop-down menus in most recently launched order. However it is possible to force a launch configuration to always appear at the top of the drop-downs by making the configuration a 'favorite'. Use the Organize Favorites... action from the appropriate drop down menu to configure your favorite launch configurations. |
Detail formatters |
In the Variables & Expressions views, the detail pane shows an expanded representation of the currently selected variable. By default, this expanded representation is the result of calling toString() on the selected object, but you can create a custom detail formatter that will be used instead by choosing New Detail Formatter from the variable's context menu. This detail formatter will be used for all objects of the same type. You can view and edit all detail formatters in the Java > Debug > Detail Formatters preference page.
|
Running code with compile errors |
You can run and debug code that did not compile cleanly. The only difference between running code with and without compile errors is that if a line of code with a compile error is executed, one of two things will happen:
It is important to emphasize that as long as your execution path avoids lines of code with compile errors, you can run and debug just as you normally do. |
Word wrap in Variables view | The details area of the debugger's Variables
and Expressions views supports word wrap, available from the
view drop-down menu.
|
Code assist in the debugger |
Code assist is available in many contexts beyond writing code in the Java editor:
|
Command line details | You can always see the exact command
line used to launch a program in run or debug mode by selecting Properties
from the context menu of a process or debug target, even if the
launch has terminated.
|
Stack trace hyperlinks |
Java stack traces in the console appear with hyperlinks. When you place the mouse over a line in a stack trace, the pointer changes to the hand and the stack trace is underlined. Pressing the mouse button opens the associated Java source file and positions the cursor at the corresponding line. Pressing the mouse button on the exception name at the top of the stack trace will create an exception breakpoint.
|