Monday, June 25, 2007

Eclipse - Generating code, constructors, getters, and setters

Sometimes we have to work with POJO, like when we work with Hibernate. The main problem is after we create all the member variables, we should create the getters and setters. To create N properties, it need N * 2 method (of getters and setters), that would be an extra job for your fingers. The same problem comes when we want to create a constructor that has parameter from the member variables . Thanks god, Eclipse come to save us.

Assume we have create UserProfile class.



Now we will create the constructor
"right click + source"
or
"alt + shift + s" on windows
or
"alt + cmd + s" on mac
then select
"Generate Constructor using Fields..."

Select all fields you need



and click OK, it should be like this



and let's generate the getter and setter, open the source menu like above, then select
"Generate Getters and Setters..."



"Select all" or what ever you need, then boom, Eclipse save your fingers.

1 comment:

Yinch Worm said...

thanks dude, that helped.