javaintellij-idea

How to auto generate Getter() below the variable in IntelliJ IDEA?


this seems to be a very silly problem but I really have no idea how to deal with it. I'm using IntelliJ IDEA to write java code. Let's say my class have a variable name:

public class city {
    private String name;
}

And I want to generate a Getter() method for name, the most convenient way is to use the auto generate:

enter image description here

However here is the result:

public class city {
    public String getName() {
        return name;
    }

    private String name;
}

As you can see, the getName() is always generated above the variable, but I want it to be below the variable. What should I do to achieve that? Thanks.


Solution

  • It depends on the position of the cursor. so where you click to generate the getter, it will create the code. to solve this. Just move the cursor under the attribute and click Alt + Insert

    enter image description here

    Then the result is:

    enter image description here