Getter / Setter generation in Java

2010-03-17  |   |  java  

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.


Name: Emmanuel Bernard
Bio tags: French, Open Source actor, Hibernate, (No)SQL, JCP, JBoss, Snowboard, Economy
Employer: JBoss by Red Hat
Resume: LinkedIn
Team blog: in.relation.to
Personal blog: No relation to
Microblog: Twitter, Google+
Geoloc: Paris, France

Tags