9

I think the creation of holder classes, classes with just attritutes, get methods and set methods can be autogenerated.

I understand that Eclipse is a defacto "standard" Java IDE these days.
Does Eclipse do this?
Do other IDEs?

I'm guessing the only reasons for making dedicated data holding classes instead of using HashMaps is to have mixed datatypes in a more visible/safter way and for the occasion when you want to DO something to the data when inserting it or retrieving it. Thoughts?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Steve
  • 3,127
  • 14
  • 56
  • 96

7 Answers7

23

Yes. Eclipse Does this. To generate setter/getter, select the class in package explorer, right click -> Source -> Generate getter/setting.

Netbean also does this. Right click anywhere inside your class -> insert code-> setter/getter.

we do that to encapsulate the implementation details. you can find more explanations Why use getters and setters

Community
  • 1
  • 1
neo
  • 2,461
  • 9
  • 42
  • 67
  • Eclipse will also offer a "Create getter and setter for" option in the quick fix menu for a field. You can get the quick fix menu for something by putting the cursor on it and hitting control-1 (command-1 on the Mac). – Tom Anderson Dec 30 '11 at 19:57
  • I wrote setters/getters by hand for 10 years before finding this answer :'-( – A. Mashreghi Jun 28 '22 at 05:03
12

Try this on Eclipse

Right click the file -> Source -> Generate Getters and Setters

enter image description here

Try this on IDEA

Right click anywhere in the file -> Generate -> Getter and Setter

enter image description here

Community
  • 1
  • 1
Fangming
  • 24,551
  • 6
  • 100
  • 90
1

In IntelliJ IDEA:

  1. Right click anywhere in the code and choose Generate...
    or
    Press Alt + Insert

Right click anywhere in the code

  1. Choose Getter and Setter

Choose Getter and Setter

  1. Select the fields you want to add Getters and Setters to and press OK.

Select the fields

Tested in IntelliJ IDEA 2022 (Ultimate Edition)

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
0

Press Alt+Shift+S, then R this is a shortcut for right click -> Source -> Generate getter/setting.

0

IntelliJ IDEA can also generate getters and setters and it is much better than Eclipse (flamesuite on!). Data holding classes can be more memory efficient than HashMaps as you can use primitives to hold values (int) instead of wrapper classes (Integer).

David Tinker
  • 9,383
  • 9
  • 66
  • 98
0
  1. Yes All (IDEA,Eclipse,Netbeas at least) the current IDEs support generating setter and getter methods.

  2. I think POJO's has lots of advantages against using just simple hash maps. Since there are frameworks that support their validation, easily can be mapped to database tables, pojo classes specifies clearly your domain model on which you are operating, could be analyzed at runtime using reflection, etc...

csviri
  • 1,159
  • 3
  • 16
  • 31
0

Most modern IDEs come with some form of code generation tools. Netbeans certainly does.

If you want a class to hold specific data, consider your need for validation. If you don't plan on validating the data (hint: you should), then skip the get/set methods and make the field public.

In the end, there's really no definite answer to this question. Java's built-in collections are useful because they're flexible and readily available, but if you want extra validation then you have to do it yourself or wrap the collection with validation methods. Objects dedicated to specific data gain the advantage of immediate validation and might have less overhead, but that's extra code to maintain and you could end up with a lot of classes that really don't do much.

Eric
  • 6,965
  • 26
  • 32