tags:

views:

502

answers:

7

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?

Help a total idiot pls, thx.

+1  A: 

My Java experience is not that high either, so anyone feel free to correct me. But AFAIK, the general convention is to write two methods like so:

public string getMyString() {
    // return it here
}

public void setMyString(string myString) {
    // set it here
}
Splash
A: 

If you're using eclipse then it has the capabilities to auto generate the getter and setter method for the internal attributes, it can be a usefull and timesaving tool.

TK
+1  A: 

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.

Hank Gay
+10  A: 

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();
Calum
I can swear I have seen the C# style property syntax somewhere in some Java code, but for the life of me I cannot remember where and how. This really does not answer my question but I'll accept it for the awesomeness factor. Perhaps I've been hallucinating back then.
Ishmaeel
I'm pretty sure it can't be done in Java, sorry. There's a lot of JVM languages which do have first-class support for this sort of thing, though, maybe that's what you saw?
Calum
+1  A: 

Most IDEs for Java will automatically generate getter and setter code for you if you want them to. There are a number of different conventions, and an IDE like Eclipse will allow you to choose which one you want to use, and even let you define your own.

Eclipse even includes automated refactoring that will allow you to wrap a property up in a getter and setter and it will modify all the code that accesses the property directly, to make it use the getter and/or setter.

Of course, Eclipse can only modify code that it knows about - any external dependencies you have could be broken by such a refactoring.

Bill Michell
+1  A: 

"Java Property Support" is proposed for Java 7.

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

Cheekysoft
A: 

I'm just releasing Java 5/6 annotations and an annotation processor to help this.

Check out http://code.google.com/p/javadude/wiki/Annotations

The documentation is a bit light right now, but the quickref should get the idea across.

Basically it generates a superclass with the getters/setters (and many other code generation options).

A sample class might look like

@Bean(properties = {
    @Property(name="name", bound=true),
    @Property(name="age,type=int.class)
})
public class Person extends PersonGen {
}

There are many more samples available, and there are no runtime dependencies in the generated code.

Send me an email if you try it out and find it useful! -- Scott

Scott Stanchfield