Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Getters and Setters

Is there a better standard way to create getters and setters in Java?

It is quite verbose to have to explicitly define getters and setters for each variable. Is there a better standard annotations approach?

Does Spring have something like this?

Even C# has properties.

like image 957
Verhogen Avatar asked Dec 15 '09 13:12

Verhogen


2 Answers

Eclipse has a context menu option that will auto-generate these for you, as I am sure many other IDE's do.

like image 44
Ascalonian Avatar answered Sep 28 '22 10:09

Ascalonian


I'm not sure if you'd consider it 'standard', but Project Lombok addresses this problem. They use annotations to replace much of the verbosity of Java.

Some people are looking at alternative Java sibling languages, such as Groovy or Scala. I'm afraid it will take some years -if at all- before the JSR figures out a "standardized" way to "fix" this in Java proper.

like image 145
Carl Smotricz Avatar answered Sep 28 '22 10:09

Carl Smotricz