Code Style. Java
File | Settings | Editor | Code Style | Java for Windows and Linux
IntelliJ IDEA | Settings | Editor | Code Style | Java for macOS
CtrlAlt0S
Use this page to configure formatting options for Java files. When you change these settings, the Preview pane shows how this will affect your code.
Item | Description |
---|---|
Use tab character | Use the Tab key for indentation. When the checkbox is cleared, IntelliJ IDEA uses spaces instead of tabs. |
Smart tabs |
The Smart tabs checkbox is available if the Use tab character option is enabled. |
Tab size | In this field, specify the number of spaces included in a tab. |
Indent | In this field, specify the number of spaces to be inserted for each indent level. |
Continuation indent | Specify the indentation for lines that continue from the previous line, making it clear that they are part of the same statement or block of code. Continuation indents are used when a single statement is too long to fit on one line. |
Keep indents on empty lines | If this checkbox is selected, IntelliJ IDEA will keep indents on the empty lines as if they contained some code. If this checkbox is cleared, IntelliJ IDEA will delete the tab characters and spaces. |
Label indent | In this field, specify the number of spaces to be inserted at the next line before a label statement. |
Absolute label indent | If this checkbox is selected, label indentation is counted as an absolute number of spaces. Otherwise, label indentation is counted relative to previous indent levels. |
Do not indent top level class members | Select this checkbox to have top level class members located at the class declaration indentation level. |
Use indents relative to expression start | Use this checkbox to switch between the two possible indentation behaviors:
|
Select or clear the checkboxes to insert, not to insert, or remove spaces in various contexts.
Item | Example |
---|---|
Method declaration parentheses | If selected, a space is inserted before the opening parenthesis in method declarations. Otherwise, no space is inserted. Selected
Not selected
|
Method call parentheses | If selected, a space is inserted before the opening parenthesis in method calls. Otherwise, no space is inserted. Selected
Not selected
|
'if' parentheses | If selected, a space is inserted before the opening parenthesis in Selected
Not selected
|
'for' parentheses | If selected, a space is inserted before the opening parenthesis in Selected
Not selected
|
'while' parentheses | If selected, a space is inserted before the opening parenthesis in Selected
Not selected
|
'switch' parentheses | If selected, a space is inserted before the opening parenthesis in Selected
Not selected
|
'try' parentheses | If selected, a space is inserted before the opening parenthesis in Selected
Not selected
|
'catch' parentheses | If selected, a space is inserted before the opening parenthesis in Selected
Not selected
|
'synchronized' parentheses | If selected, a space is inserted before the opening parenthesis in Selected
Not selected
|
Annotation parameters | If selected, a space is inserted before the opening parenthesis of annotation parameters. Otherwise, no space is inserted. Selected
Not selected
|
Deconstruction list | If selected, a space is inserted before the opening parenthesis in deconstruction patterns. Otherwise, no space is inserted. Selected
Not selected
|
Item | Example |
---|---|
Assignment operators (=, +=, ...) | If selected, spaces are inserted around assignment operators in assignment expressions. Otherwise, no spaces are inserted. Selected
Not selected
|
Logical operators (&&, ||) | If selected, spaces are inserted around logical operators in logical expressions. Otherwise, no spaces are inserted. Selected
Not selected
|
Equality operators (==, !=) | If selected, spaces are inserted around equality operators in comparison expressions. Otherwise, no spaces are inserted. Selected
Not selected
|
Relational operators (<,>,<=,>=) | If selected, spaces are inserted around relational operators in comparison expressions. Otherwise, no spaces are inserted. Selected
Not selected
|
Bitwise operators (&, |, ^) | If selected, spaces are inserted around bitwise operators. Otherwise, no spaces are inserted. Selected
Not selected
|
Additive operators (+, -) | If selected, spaces are inserted around additive operators. Otherwise, no spaces are inserted. Selected
Not selected
|
Multiplicative operators (*, /, %) | If selected, spaces are inserted around multiplicative operators. Otherwise, no spaces are inserted. Selected
Not selected
|
Shift operators (<<, >>, >>>) | If selected, spaces are inserted around bit shifting operators. Otherwise, no spaces are inserted. Selected
Not selected
|
Unary operators (!,-,+,++,--) | If selected, spaces are inserted around unary operators. Otherwise, no spaces are inserted. Selected
Not selected
|
Lambda arrow | If selected, spaces are inserted around lambda arrows. Otherwise, no spaces are inserted. Selected
Not selected
|
Method reference double colon | If selected, a space is inserted around method reference double colons. Otherwise, no space is inserted. Selected
Not selected
|
Item | Example |
---|---|
Class left brace | If selected, a space is inserted between the class name and the opening brace in class declarations. Otherwise, no space is inserted. Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for In class declaration is set to End of line on the Wrapping and Braces tab. |
Method left brace | If selected, a space is inserted between the method parameters' list closing parenthesis and the opening brace in method declarations. Otherwise, no space is inserted. Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for In method declaration is set to End of line on the Wrapping and Braces tab. |
'if' left brace | If selected, a space is inserted between the conditional expression's closing parenthesis and the opening brace in Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
'else' left brace | If selected, a space is inserted between the Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
'for' left brace | If selected, a space is inserted between the conditional expression's closing parenthesis and the opening brace in Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
'while' left brace | If selected, a space is inserted between the conditional expression's closing parenthesis and the opening brace in Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
'do' left brace | If selected, a space is inserted between the Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
'switch' left brace | If selected, a space is inserted between the conditional expression's closing parenthesis and the opening brace in Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
'try' left brace | If selected, a space is inserted between the Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
'catch' left brace | If selected, a space is inserted between the Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
'finally' left brace | If selected, a space is inserted between the Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
'synchronized' left brace | If selected, a space is inserted between the Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
Array initializer left brace | If selected, a space is inserted before the left brace of an array initializer. Otherwise, no space is inserted. Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
Annotation array initializer left brace | If selected, a space is inserted before the left brace of an array initializer. Otherwise, no space is inserted. Selected
Not selected
Selecting or clearing the checkbox is relevant only when Braces placement for Other is set to End of line on the Wrapping and Braces tab. |
Item | Description |
---|---|
'else' keyword | If selected, a space is inserted between the closing brace of an Selected
Not selected
|
'while' keyword | If selected, a space is inserted between the closing brace of a Selected
Not selected
|
'catch' keyword | If selected, a space is inserted between the closing brace of a Selected
Not selected
|
'finally' keyword | If selected, a space is inserted between the closing brace of a Selected
Not selected
|
Item | Description |
---|---|
Code braces | If selected, spaces within code braces are always inserted. Otherwise, spaces are never inserted. |
Brackets | If selected, spaces within brackets are always inserted. Otherwise, spaces are never inserted. Selected
Not selected
|
Array initializer braces | If selected, spaces inside parentheses in array initializer expressions are always inserted. Otherwise, no spaces are inserted. Selected
Not selected
|
Empty array initializer braces | If selected, spaces are inserted within the braces of an empty array initializer Selected
Not selected
|
Grouping parentheses | If selected, spaces inside grouping parentheses in complex expressions are always inserted. Otherwise, no spaces are inserted. Selected
Not selected
|
Method declaration parentheses | If selected, spaces are inserted within method declaration parentheses. Otherwise, no spaces are inserted. Selected
Not selected
|
Empty method declaration parentheses | If selected, spaces are inserted within the empty method declaration parentheses. Otherwise, no spaces are inserted. Selected
Not selected
|
Method call parentheses | If selected, spaces are inserted within method call parentheses. Otherwise, no spaces are inserted. Selected
Not selected
|
Empty method call parentheses | If selected, spaces are inserted within the empty method call parentheses. Otherwise, no spaces are inserted. Selected
Not selected
|
'if' parentheses | If selected, spaces inside parentheses in Selected
Not selected
|
'for' parentheses | If selected, spaces inside parentheses in Selected
Not selected
|
'while' parentheses | If selected, spaces inside parentheses in Selected
Not selected
|
'switch' parentheses | If selected, spaces inside parentheses in Selected
Not selected
|
'try' parentheses | If selected, spaces inside parentheses in Selected
Not selected
|
'catch' parentheses | If selected, spaces inside parentheses in Selected
Not selected
|
'synchronized' parentheses | If selected, spaces inside parentheses in Selected
Not selected
|
Type cast parentheses | If selected, spaces inside type cast parentheses are always inserted. Otherwise, no spaces are inserted. Selected
Not selected
|
Annotation parentheses | If selected, spaces inside annotation parentheses are always inserted. Otherwise, no spaces are inserted. Selected
Not selected
|
Angle brackets | If selected, spaces inside angle brackets are always inserted. Otherwise, no spaces are inserted. Selected
Not selected
|
Record header | If selected, spaces inside record headers are always inserted. Otherwise, no spaces are inserted. Selected
Not selected
|
Deconstruction list | If selected, spaces inside deconstruction lists are always inserted. Otherwise, no spaces are inserted. Selected
Not selected
|
Inside block braces when body is present | If selected, spaces inside deconstruction lists are always inserted. Otherwise, no spaces are inserted. Selected
Not selected
|
Item | Description |
---|---|
Before '?' After '?' Before ':' After ':' Between '?' and ':' | Select the checkboxes in this section to have spaces automatically inserted around For example, if Before '?' and After ':' are selected, IntelliJ IDEA automatically inserts spaces before Selected
Not selected
|
Item | Description |
---|---|
After comma | If selected, spaces are inserted after commas in type arguments with multiple types. Otherwise, no spaces are inserted. Selected
Not selected
|
Before opening angle bracket | If selected, spaces are inserted before opening angle brackets in type arguments. Otherwise, no spaces are inserted. Selected
Not selected
|
After closing angle bracket | If selected, spaces are inserted after closing angle brackets in type arguments. Otherwise, no spaces are inserted. Selected
Not selected
|
Item | Description |
---|---|
Before comma | If selected, spaces are automatically inserted before commas in parameter lists, argument lists, array declarations, and so on. Otherwise, no spaces are inserted. Selected
Not selected
|
After comma | If selected, spaces are automatically inserted after commas in parameter lists, argument lists, array declarations, and so on. Otherwise, no spaces are inserted. Selected
Not selected
|
Before 'for' semicolon | If selected, spaces are automatically inserted before semicolons in Selected
Not selected
|
After 'for' semicolon | If selected, spaces are automatically inserted after semicolons in Selected
Not selected
|
After type cast | If selected, a space is automatically inserted after the closing parenthesis of a cast. Otherwise, no space is inserted and the casted variable sticks to the cast. Selected
Not selected
|
Around '=' in annotation value pair | If selected, spaces are automatically inserted around the Selected
Not selected
|
Before colon in foreach | If selected, a space is automatically inserted before a colon in Selected
Not selected
|
Inside one line enum braces | If selected, spaces are automatically inserted inside the braces of one-line enums around the list of constants. Otherwise, no spaces are inserted. Selected
Not selected
|
Item | Description |
---|---|
Before opening angle bracket | If selected, a space is automatically inserted before an opening angle bracket of a type parameter. Otherwise, no space is inserted. Selected
Not selected
|
Around type bounds | If selected, spaces are automatically inserted around type bounds of a type parameter. Otherwise, no space is inserted. Selected
Not selected
|
In this tab, customize the code style options, which IntelliJ IDEA will apply on reformatting the source code. The left-hand pane contains the list of exceptions (Keep when reformatting), and placement and alignment options for the various code constructs (lists, statements, operations, annotations, and so on) The right-hand pane shows the preview.
Alignment takes precedence over indentation options.
Use the Hard wrap at field to specify a margin space required on the right side of an element. If you select the Default option, then a value of the right margin from the global settings is used.
Use the Wrap on typing settings to specify how the edited text is fitted in the specified Hard wrap at:
Default: in this case IntelliJ IDEA uses the Wrap on typing option that is specified in the global settings.
Yes: in this case IntelliJ IDEA uses the value specified in the Right Margin field.
No: in this case this option is switched off and a line can exceed the value specified in the right margin.
Use the Visual guides field to specify multiple right margins. You can leave a default value or enter the number of spaces for your margin. If you want to specify several margins, enter numbers separated by comma.
Use the checkboxes to configure exceptions that IntelliJ IDEA will make when reformatting the source code. For example, by default, the Line breaks checkbox is selected. If your code contains lines that are shorter than a standard convention, you can convert them by disabling the Line breaks checkbox before you reformat the source code.
The wrapping style applies to the various code constructs, specified in the left-hand pane (for example, method call arguments, method declaration parameters, or assignment statements).
Item | Description |
---|---|
Wrapping style | From this list, select the desired wrapping style:
|
Item | Description |
---|---|
Align when multiline | If this checkbox is selected, a code construct starts at the same column on each next line. Otherwise, the position of a code construct is determined by the current indentation level. |
| Select this checkbox to have the specified character or characters moved to the next line when the lines are wrapped. |
'else' on new line | Use this checkbox to have the corresponding statements or characters moved to the next line. |
New line after | Select this checkbox to have the code after the specified character moved to a new line. |
Special 'else if' treatment | If this checkbox is selected, Otherwise, |
Indent 'case' branches | If this checkbox is selected, the |
Item | Description |
---|---|
Braces placement style | Use this list to specify the position of the opening brace in class declarations, method declarations, and other types of declarations. The available options are:
|
Force braces | From this list, choose the braces introduction method for
|
Use the following options to format chained method calls and make them easier to read. Note that builder method calls are always wrapped regardless of the settings for chained calls.
Item | Description |
---|---|
Wrap first call | Allow wrapping the first method call in chained methods. |
Align when multiline | Align several method calls. |
Builder methods | Specify comma-separated names (identifiers) of methods that you want to be treated as builder methods. For example: |
Keep builder methods indents | Keep additional indents that you insert manually intact as you reformat code. |
Use this tab to define where and how many blank lines you want IntelliJ IDEA to retain and insert in your code after reformatting. For each type of location, specify the number of blank lines to be inserted. The results are displayed in the preview pane.
Item | Description |
---|---|
Keep maximum blank lines | In this area, specify the number of blank lines to be kept after reformatting in the specified locations. |
Minimum blank lines | In this area, specify the number of blank lines to be present in the specified locations.
|
Item | Description |
---|---|
Alignment | Define the way Javadoc comments should be aligned.
|
Blank lines | Define where blank lines should be inserted in Javadoc comments.
|
Invalid tags | In this area, define whether invalid tags should be preserved or not.
|
Other | In this area, specify additional formatting options for Javadoc comments.
|
This tab lets you define a set of rules that rearranges your code according to your preferences.
note
Code arrangement is not a part of code reformatting. To rearrange your code, run the dedicated action or configure the IDE to automatically rearrange code on save.
Item | Description |
---|---|
Grouping Rules | Use this area to set the grouping rules.
|
Matching rules | Use this area to define elements order as a list of rules, where every rule has a set of matches such as modifier or type.
|
Empty rule | Use this area to create a new matching rule or edit an existing one. You can select from the following filters:
|
This icon appears when you select Order by Name from the Order list. The icon indicates that the items in this rule are sorted alphabetically. |
This table lists actions to be performed when imports are optimized.
Item | Description |
---|---|
General | In this area, configure general import options. Options:
|
JSP Imports Layout | In this area, configure how JSP import statements should be organized in your code. The introduced changes are displayed in the Preview pane below. Options:
|
Packages to Use Import with '*' | In this area, configure a list of packages and classes to be always imported completely. Options:
|
Import Layout | In this area, configure how import statements should be organized in your code. You can set up certain classes to be positioned first, or last, or one after another. Imported classes will be grouped as per their packages and sorted alphabetically within a package. Options:
|
Item | Description |
---|---|
Naming |
|
Default Visibility | Select the default access level for generated fields and methods. You can either specify it explicitly, or select Escalate to automatically raise it to a necessary level. |
Variable declaration | Specify whether you want to generate local variables and parameters with the |
Comment Code | Use this area to configure code style for generated comments (line Ctrl0/ and block CtrlShift0/):
|
Override Method Signature |
|
Lambda Body | If a lambda expression calls an existing method, it is preferable to refer to the method by name using a method reference. These checkboxes affect the Lambda can be replaced with method reference inspection. If enabled, the corresponding lambda expressions will be highlighted as warnings with a relevant quick-fix. If disabled, the code will not be highlighted, but an intention to replace the lambda expression with a method reference will still be available.
|
Thanks for your feedback!