(no) Properties in Java?

So, I have willfully kept myself a Java n00b until recently, and my first real exposure brought about a minor shock: Java does not have C# style properties!

Ok, I can live with that. However, I can also swear that I have seen property getter/setter code in Java in one codebase, but I cannot remember where. How was that achieved? Is there a language extension for that? Is it related to NetBeans or something?


Solution 1:

There is a "standard" pattern for getters and setters in Java, called Bean properties. Basically any method starting with get, taking no arguments and returning a value, is a property getter for a property named as the rest of the method name (with a lowercased start letter). Likewise set creates a setter of a void method with a single argument.

For example:

// Getter for "awesomeString"
public String getAwesomeString() {
  return awesomeString;
}

// Setter for "awesomeString"
public void setAwesomeString( String awesomeString ) {
  this.awesomeString = awesomeString;
}

Most Java IDEs will generate these methods for you if you ask them (in Eclipse it's as simple as moving the cursor to a field and hitting Ctrl-1, then selecting the option from the list).

For what it's worth, for readability you can actually use is and has in place of get for boolean-type properties too, as in:

public boolean isAwesome();

public boolean hasAwesomeStuff();

Solution 2:

I am surprised that no one mentioned project lombok

Yes, currently there are no properties in java. There are some other missing features as well.
But luckily we have project lombok that is trying to improve the situation. It is also getting more and more popular every day.

So, if you're using lombok:

@Getter @Setter int awesomeInteger = 5;

This code is going to generate getAwesomeInteger and setAwesomeInteger as well. So it is quite similar to C# auto-implemented properties.

You can get more info about lombok getters and setters here.
You should definitely check out other features as well. My favorites are:

  • val
  • NoArgsConstructor, RequiredArgsConstructor, AllArgsConstructor
  • Logs!

Lombok is well-integrated with IDEs, so it is going to show generated methods like if they existed (suggestions, class contents, go to declaration and refactoring).
The only problem with lombok is that other programmers might not know about it. You can always delombok the code but that is rather a workaround than a solution.

Solution 3:

"Java Property Support" was proposed for Java 7, but did not make it into the language.

See http://tech.puredanger.com/java7#property for more links and info, if interested.

Solution 4:

The bean convention is to write code like this:

private int foo;
public int getFoo() {
    return foo;
}
public void setFoo(int newFoo) {
    foo = newFoo;
}

In some of the other languages on the JVM, e.g., Groovy, you get overridable properties similar to C#, e.g.,

int foo

which is accessed with a simple .foo and leverages default getFoo and setFoo implementations that you can override as necessary.