Java code style question


We switched to Intellij IDEA some time ago and are really happy with the product. I used Eclipse the last years and have a question to code style formatting. Two problems so far, which I couldn't get solved while using a lot of different settings at the IDE. Both occur while reformat the code with "Ctrl + Alt + L". The first problem is the formatting of the block of class variables.

In Eclipse they were formatted like this:

private static                    dummyClass instance;
private int                       dummyInt        = 0;
private booelan                   dummyBool       = false;
private static String             dummyString     = null;
private ArrayList<String>         stringArrayList = new ArrayList<>();
private HashMap<String, Computer> hashMap         = new HashMap<>();

In IntelliJ IDEA they are formatted like this:

private static dummyClass instance;
private int dummyInt = 0;
private booelan dummyBool = false;
private static String dummyString = null;
private ArrayList<String> stringArrayList = new ArrayList<>();
private HashMap<String, Computer> hashMap = new HashMap<>();

For me, it's much more harder to read and the code doesn't look good. Can someone help me, which code style settings I have to enable, to get a formatted code like above? Like I said, I tried a lot of settings, but didn't get the same style.

The second problem is, that lines with comments will not be reformatted. They will stay on the position I original created them. For example, I copy & paste a code block which is not properly intended. "Ctrl + Alt + L" will reformat the code and intend it properly, only the comment will left on the wrong position:

if (!string.isEmpty() && count < array.size()) {
            /* Some comment here */
    System.out.println(count + ":" + string);

Should look like:

if (!string.isEmpty() && count < array.size()) {
    /* Some comment here */
    System.out.println(count + ":" + string);

Any idea which settings are responsible for these code style settings?

Comment actions Permalink


Settings -> Code Style -> Java -> Wrapping and Braces -> Group declarations
[ ] Align fields in columns
[ ] Align variables in columns

I am not sure though if the format will completely equal that of Eclipse.

I am not sure about the comment indenting. I guess it is a bug.

With line style comments
     // some comments here
it works correctly.

Comment actions Permalink

Stephen, the settings for alignment are what I was looking for! :)
Maybe they are not completely equal to that of Eclipse, but I'm perfectly satisfied.

Unfortunately, line style comments does'nt work in my case too...

Nevertheless I'm happy with the alignment settings, thank you!

Comment actions Permalink

I know this is a bit late, but there's a plugin that can run an exported Eclipse formatter config. (


Please sign in to leave a comment.