Generate getters and setters in Eclipse IDE

Still busy writing setters and getters method manually? Actually, Eclipse IDE is able to help you generate the setters and getters method automatically, and this feature is bundle with Eclipse IDE long time ago, surprisingly, many Java developers are not aware of it?

See following steps to show you how.

1. Java Pojo

A simple User.java, with 4 fields, but no getters and setters method.

Java pojo

2. Generate It

Right click on the file, select “Source” –> “Generate Getters and Setters…

getters and setters in eclipse ide

Choose which field you want to generate, and click on “OK” button.

getters and setters in eclipse ide

3. Done

All selected getters and setters methods will be generated automatically.

getters and setters in eclipse ide

So, don’t create those methods manually, it waste time, use IDE.

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • andrew

    wow, that was so simple and straight to the point. thanks!

  • Michael Diman

    Use Lombok
    and use @Data as annotation

  • venkat

    tanq

  • clement

    There’s an error in part 2, second screenshot. It is “field” and not “filed” in the phrase “which filed you want to generate”.

  • Alex

    I mean using java programming language.

  • Alex

    Sir how can i write HTML tags in between a html table

    for example

    line 1 : html table tags

    line 2 : data to be written

    line 3 : html table tags

  • http://none Chris

    Hi Mk Yong, I wonder why you don’t user NetBeans IDE to do this all stuff with java.
    WebServices, JSF 2.0, Hibernate is more more more simply with NetBeans

    • http://www.mkyong.com mkyong

      No, I don’t like NetBean, slow and weird interface. I’m big fan of Eclipse ~

  • felix

    are there some shortcut key to do this in batch?
    alt + shift + s can open the first window
    but how can I do the next without mouse?

    • http://www.NinthAvenue.com.au Roger Keays

      With Lombok you don’t even need to generate getters and setters. A couple of class-level annotations and you’re done!

      • http://www.mkyong.com mkyong

        Issit? Mind to share which annotation and is this compatible with others framework? As i Know, most frameworks still required set and get methods, to inject and retrieve the value.

        • Michael Diman

          Use Lombok
          and use @Data as annotation

    • x

      ctrl+shift+g

    • http://sukrucakmak.com Sukru

      Alt + Shift + S and then press R

  • Serhat

    Another way:
    go over the field then press CTRL + 1, and select “Create getter and setter…”. Done!

  • josemm

    Or using lombok :P

    • Mike

      +1