Eclipse Tip: Creating Getters and Setters

Creating getters and setters are probably the most common methods in
Java. It is trivial to write but is does take some time. Eclipse has a
couple a ways to speed up the process. But strangely I do not see a lot
of developers using this. I know some type very fast and can probably
beat the code generation but for the rest us, here they are.

The first way is useful when you have multiple class level variables for which you require to code getters and setters. Go to Source > Generate Getter and Setter…
either from the main menu or context menu. A dialog will pop up with
all the possible getters and setters. Here you have options to choose
which getters and setters you what to generate, where you what to
insert them, how you what to sort them, what access modifiers you want
to use… You can also choose if you want comments to be generated.
Additionally you can change the code templates to suit your needs.

The second way is useful if you want to generate getter and setter for a variable you just added. Just type set and press Crtl + Space.
Eclipse will give you auto complete options for the setter method. The
same goes for the getter method. Here the methods are created with public access and the comments are generated (or not) based on your project preferences.

Vasanth Dharmaraj is a Java Architect currently working for Western Union living in the Bay Area. He is interested in programming, gadgets, formula one... You can follow him on Twitter.

Posted in Eclipse, Java
One comment on “Eclipse Tip: Creating Getters and Setters
  1. Prathiraj says:

    I’ve been using Windows Live mail for quite sometime and I love the latest release which comes with a load of features. Apart from the spell cheker, keyboard shortcuts is also one of my favourites and not to mention the adjustable panes. Wish you to get an invite soon. [It's a massive move and team Live mail is putting in loads of hours making it possible]. For timely updates keep checking Imran Qureshi’s blog at []

Ads By Google