In the Java editor press Ctrl+Shift+F on code to format it. If no selection is set then the entire source is formatted otherwise only the selection will be. Some tips for using the formatter are listed in the paragraphs of this chapter.
Note that the Java Formatter preferences are accessible on the Java Formatter preference page.
You can disable/enable the formatter in one or several sections in the code as shown in the sample below:
The snippet above use default tag names, but they can be changed on the Off/On tags tab of the Java Formatter preference page.
Since version 3.6, the Java formatter now tries to wrap the outermost method calls first to have a better output when wrapping nested method calls.
Here is an example of a formatted code where the formatter has wrapped the line between the arguments of the outermost message call to keep each nested method call on a single line:
A new preference allows you to disable this strategy, typically if you want to format your code as before, then uncheck the Prefer wrapping outer expressions preference accessible on the Line wrapping tab of the Java Formatter preference page.
Note: Currently the new strategy only applies to nested method calls, but that might be extended to other nested expressions in future versions.
Users can reduce the number of lines of formatted multi-lines comments as shown in the example below:
To activate this behavior uncheck the /* and */ on separate lines preference accessible on the Comments tab of the Java Formatter preference page.
The same kind of preference is also available for the Javadoc comments.
Users can preserve line breaks by not joining lines in code or comments.
For example, the already wrapped lines of the return
statement in the following test case:
will be preserved by the formatter when the Never join lines preference is used, hence produces the following output when formatted:
To activate this behavior check the Never join lines preference accessible on the Line Wrapping and the Comments tabs of the Java Formatter preference page.