Question

Like in Scala? Is there any pattern in Java to avoid having all the boilerplate setter/getter without using 3rd party jars? Thanks

update:

my aim is to avoid having too many ghost methods for Dtos, thanks

Was it helpful?

Solution

If all you want is to avoid having to type, use auto generation feature of IDEs like some others mentioned.

If you want to avoid seeing getter/setter in your classes, use a library called lombok which can generate the getters/setters behind the scene

If the above options are not OK for you and you need to set the value only once, you can declare all your fields as public final and have a constructor setting the values.

CAUTION: I am not suggesting this third option as a good practice as it breaks the Javabeans convention. Also it exposes your class' internal structure, but honestly, even with prolific use of getters, you are exposing class fields to the client.

OTHER TIPS

There are a lot of IDE's out there for java that will help you with this problem.

In Eclipse, when you right click on a variable, you can choose Source -> Generate Getters and Setters.

I never used Scala but this should do.

enter image description here

in idea intellij u can create as AlT+ insert and then choose getter+setter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top