Java getters and setters

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

It is sufficient to specify getters and setters for each variable in detail. Is there a more efficient approach to annotations?

Does Spring have something like this?

Even C # has properties.

+51
java annotations getter-setter
Dec 15 '09 at 13:04
source share
15 answers

I'm not sure if you think this is standard, but the Lombok Project addresses this issue. They use annotations to replace most of the verbosity of Java.

Some people are looking for alternative Java-related languages ​​like Groovy or Scala. I am afraid it will take several years - even if the JSR figure out a "standardized" way to "fix" this in Java.

+58
Dec 15 '09 at 13:11
source share

Eclipse has a context menu option that will automatically generate them for you, as I'm sure many other IDEs do.

+18
Dec 15 '09 at 13:10
source share

I have created several annotations that are not dependent on eclipse.

See http://code.google.com/p/javadude/wiki/Annotations

For example:

package sample; import com.javadude.annotation.Bean; import com.javadude.annotation.Property; import com.javadude.annotation.PropertyKind; @Bean(properties={ @Property(name="name"), @Property(name="phone", bound=true), @Property(name="friend", type=Person.class, kind=PropertyKind.LIST) }) public class Person extends PersonGen { } 

My annotations generate a superclass; I think Lombok modifies the actually compiled class (which is not officially supported by Sun and may break - I may be wrong in how it works, but based on what I saw, they should do it)

Enjoy it! - Scott

+6
Dec 22 '09 at 18:50
source share

Most IDEs provide a shortcut for generating code (e.g. Eclipse: right click β†’ source β†’ generate recipients and setters ), although I understand that this is probably not the answer you are looking for.

Some IOC structures allow you to annotate properties so that they can be used in the context of a structure, for example. Tapestry IOC and the latest Spring, I think (but this use is limited for wireframe use)

+3
Dec 15 '09 at 13:10
source share

Here are some interesting related articles: http://cafe.elharo.com/java/why-java-doesnt-need-properties-it-already-has-them/

I think that properties are a shortcut, but this is more of a small function than a real important function.

+3
Dec 15 '09 at 13:11
source share

I agree that getter / seters are verbose. The Lombok project has a good answer for him, as suggested by others. Otherwise, you can use the capabilities of the IDE to create them.

+3
Dec 22 '09 at 20:29
source share

With Netbeans, just start typing get or set, where the getter / setter should be replaced, and invoke automatic completion (Ctrl + Space), this will give you the option to generate a getter or setter. It will also give you the ability to generate a constructor.

+2
Dec 15 '09 at 13:13
source share

As an alternative, have you tried Scala ? It compiles to Java bytecode and has many interesting shortcuts that can make your life easier for a Java programmer.

Properties, for example:

 case class Person(var name:String, var age:Int); val p = Person("John", 4) p.name p.name = "Charlie" p.name 

And the conclusion:

 defined class Person p: Person = Person(John,4) res7: String = John res8: String = Charlie 
+2
Dec 15 '09 at 13:52
source share

There is also a Spring Roo project with @RooJavaBean annotation. It also has @RooToString and @RooHashCodeEquals or something like that. It generates an AspectJ file in the background with the proper methods.

+2
May 15 '14 at 20:11
source share

Yes, you're out of luck. Groovy generates them for you, but there are no cubes in standard java. If you use Eclipse, you can generate them quite easily , as well as hashCode () and equals ().

+1
Dec 15 '09 at 13:10
source share

There is no better way for a part of the language - nothing like the keyword "property".

One alternative, as other people have said, is to use your IDE to create them. Another, if you have many objects that need it, is to write your own code generation tool that takes a base class and creates a wrapper with getters and setters.

You can also just set the variables as public. However, on the way this will probably come back to hurt you when you decide to add validation logic.

However, one final thought: if your classes are not just used for data transfer, they probably should not reveal their internal state. IMO, getter and setter classes of behavior are code smells.

+1
Dec 15 '09 at 13:14
source share

What does the IDE work to generate repeating verbose code like getters / seters

0
Dec 15 '09 at 14:14
source share

Use your IDE to generate one for you, and try to minimize the number of recipients / setters you have - you are likely to take the added benefit of immutability.

I like the C # syntax for properties, I think it's pretty and clean, and pretty clean.

0
Dec 16 '09 at 5:01
source share

Well, one option is not to be afraid of public fields. For simple classes, which, as you know, will never perform validation or additional work under the hood when receiving and configuring, public fields require fewer templates, are syntactically better and more efficient.

0
Dec 16 '09 at 5:08
source share

If you are using emacs, it is possible to define an emacs macro that will do this for you. Any emacs gurus? :)

0
Dec 22 '09 at 18:54
source share



All Articles