Eclipse and J2SE 5.0

Since release 3.1, Eclipse includes full support for the new Java language features of J2SE 5.0. One of the most important consequences of this support is that you may not notice it at all--everything you expect to work for J2SE 1.4, including editing, compiling, debugging, quick fixes, refactorings, source actions, searching, etc., will work seamlessly with J2SE 5.0's new types and syntax. In this document, we will introduce some of the more interesting capabilities Eclipse users will find when working with J2SE 5.0.

Note that both version numbers '1.5' and '5.0' are used to identify the release of the Java 2 Platform Standard Edition. Version '5.0' is the product version, while '1.5' is the developer version and is also used for the compliance level.


In order to develop code compliant with J2SE 5.0, you will need a J2SE 5.0 or J2SE 6.0 Java Runtime Environment (JRE). If you start Eclipse for the first time using a J2SE 5.0 JRE, then it will use it by default. Otherwise, you will need to use the Opens the Installed JRE preference page Java > Installed JREs preference page to register it with Eclipse.

This document introduces some of the new language features in J2SE 5.0 very briefly, but it is not a proper tutorial for these features. See here for more information.

Compiler Compliance Level

To use the new J2SE 5.0 features, you must be working on a project that has a 1.5 compliance level enabled and has a 5.0 JRE. New projects will automatically get 1.5-compliance when choosing a 5.0 JRE on the first page of the Opens the new Java project wizard New Java Project wizard:

New Project Wizard

To convert an existing J2SE 1.4 project to J2SE 5.0, you can simply:
  1. Make sure you have a J2SE 5.0 JRE installed.
  2. Start using the 5.0 features in your code.
  3. When a compile error is flagged, use Quick Fix to update the project's compliance level:

    Quick fix to enable J2SE 5.0

  4. For more fine-tuned control, the compiler compliance level can be set globally for a workspace (with the Opens the Java compiler preference page Java > Compiler preference page) or individually for each project (from the project's context menu, choose Properties > Java Compiler).

    Compiler property page

Projects with different compliance levels can co-exist in the workspace, and depend on each other. You can also fine-tune the kinds of compile warnings and errors produced for each project using Properties > Java Compiler > Errors/Warnings. The Generic Types and the Annotations section contain options added for J2SE 5.0.

Generic Types

Generic types allow objects of the same class to safely operate on objects of different types. For example, they allow compile-time assurances that a List<String> always contains Strings, and a List<Integer> always contains Integers.

Anywhere that Eclipse handles a non-generic type, it can handle a generic type: In addition, a new refactoring has been added: Infer Generic Type Arguments can infer type parameters for every type reference in a class, a package, or an entire project.

Invoking Infer Generic Type Arguments

Invoking the refactoring produces:

Invoked Infer Generic Type Arguments

Eclipse provides new options when searching for references to generic types. Consider this example:

Example with four different parameterized instantiations of List

Selecting the reference to List<Integer> and using Search > References > Project from the context menu will highlight the List types on all four lines:

Search with no filters

Using the Search View menu, the results can be filtered:

Filter Incompatible Type Arguments leaves only references to types that are assignment-compatible with the selected type:

Filter incompatible

Filter Inexact Type Arguments leaves only type references with the exact same signature:

Filter inexact


Annotations attach metadata about how Java types and methods are used and documented to the Java source and can then affect compilation or be queried at run-time. For example, @Override will trigger a compile warning if the annotated method does not override a method in a superclass:

Override annotation

Everything you can do with a Java type, you can do with an annotation:

A very useful annotation with full support in Eclipse is @SuppressWarnings. For example, consider a private method that is currently unused, but you'd rather not delete:

Warning: unused private method

Invoking quick fix on the warning proposes adding a @SuppressWarnings annotation:

SuppressWarnings proposed

Selecting the quick fix adds the annotation. The Eclipse compiler honors the annotation by removing the warning on foo:

SuppressWarnings inserted


Enumerations are types that are instantiated at runtime by a known, finite set of objects:

enum type

Again, anything you can do to a Java class can be done to an enumeration:


Autoboxing and auto unboxing allow for elegant syntax when primitive types are assigned to or retrieved from Object references:

autoboxing highlighted

Eclipse's source manipulation features handle autoboxing seamlessly, giving the correct types to new local variables and correct code assists. For code understanding, it is also possible to flag instances of autoboxing or autounboxing conversions:

Enhanced for loop

For the common case of operating on each element of an array or collection in turn, J2SE 5.0 allows a new, cleaner syntax. Eclipse provides a "foreach" code template that can automatically guess the collection to be iterated:

foreach template proposed

Choosing the template produces:

foreach template inserted

Eclipse also provides a "Convert to enhanced for loop" quick-assist to upgrade 1.4-style for loops where possible.

Static Imports

Static imports allow you to use static fields and methods from other classes without qualification.

Content assist in the Java editor can suggest such static members and adds a static import when required. To get such proposals, configure the static import favorites on the Opens the Content Assist Favorites preference page Java > Editor > Content Assist > Favorites preference page.

Converting J2SE 1.4 code to 5.0

Eclipse offers the following tools to help you bring J2SE 1.4 code to 5.0: Happy coding!