How to create Java getters and setters in Eclipse

How to effectively create Java getters and setters in Eclipse. Easily generate getters and setters.

You can use CONTROL + Space and choose appropriate getters and setters. Or even better if I use this method, I type get and then press CONTROL + Space and Eclipse will offer me to generate getters. Or I type set followed by CONTROL + Space and Eclipse will offer me to generate setters.

This is fine for adhoc generation of few getters and setters, but if your class has many attributes,this approach would be inefficient.

You can generate all getters and setters in one place. Click on Source -> Generate Getters and Setters and in this dialog you can choose what to generate. I usually select all and make sure that these checkboxes are selected: that generated methods will be final and that Eclipse will generate comments for them. Click OK.

Unfortunately this operation is by default not accessible using keyboard shortcut, but you can easily fix that. Click Window -> Preferences and choose General -> Keys. Find command „Generate getters and setters“ and type a shortcut, that will activate this operation. I usually use CONTROL + SHIFT + G. And now if I press CONTROL + SHIFT + G, Eclipse will open this dialog.

comments powered by Disqus


Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects: