Getter / Setter generation in Java

less than 1 minute read

Eclipse or IntelliJ typically generate getters and setters this way:

public class Month {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

And we all complain because that consumes a lot of screen space (9 lines). Provided that the trio attribute / getter / setter really represents a unique property, why not generate it that way?

public class Month {

    public String getName() { return this.name; }
    public void setName(String name) { this.name = name; }
    private String name;

}

That's much more compact (3 lines, ie a third of what we had) and put all related constructs as a single block. You can also mutualize the JavaDoc for free :)

What do you think?

Oh and please don't tell me about Scala, Groovy or Fantom, we all know Java should have had first-class properties. But that's life.

Tags:

Updated:

Comments