Java Getters and Setters

Solution 1:

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.

Solution 2:

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

Solution 3:

I've created some annotations that are not eclipse-specific.

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 actual class being compiled (which is officially not supported by Sun and may break - I could be wrong about how it works, but based on what I've seen they must be doing that)

Enjoy! -- Scott

Solution 4:

Here is an interesting articles about the subject: http://cafe.elharo.com/java/why-java-doesnt-need-properties-it-already-has-them/

I think properties are a shortcut but it's more a little feature than a real important feature