lomaxx
lomaxx

Reputation: 115773

Does Java have Automatic Properties?

In c# you can setup properties like this:

public int CustomerId {get;set;}

Which sets up an automatic property called CustomerId, but I was wondering if there was anything similar in Java?

Upvotes: 26

Views: 14095

Answers (5)

Jon Skeet
Jon Skeet

Reputation: 1500385

No, Java has nothing similar at the moment. Heck, properties in Java are mostly just conventions of get/set methods, rather than being genuinely understood by the compiler as they are in C#. Tools and libraries recognise the get/set pattern, but the language doesn't know about them. (It's possible that in a future version of Java, there'll be more "formal" support.)

Some Java-like languages such as Groovy do have automatic property generation, however.

Upvotes: 29

Stefan
Stefan

Reputation: 12260

  • The JavaFX properties might also be of interest:

http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm

    IntegerProperty num = new SimpleIntegerProperty(666);
    System.out.println(num.getValue());
  • Also see this related question on how to avoid get/set boiler plate code:

"Special attributes/properties" instead of getter/setter in Java to avoid boiler plate code

Upvotes: 1

David Baakman
David Baakman

Reputation: 121

Not in the Java language itself. However, there is at least one library that provides that. See: http://projectlombok.org/ (or more specific: http://projectlombok.org/features/GetterSetter.html)

Upvotes: 6

user8681
user8681

Reputation:

You can also do this easily, using the annotations from Project Lombok

Upvotes: 4

Joey
Joey

Reputation: 354476

No, there isn't such a thing in Java.

In Eclipse, however, you can automatically implement getter/setter methods for fields with Source > Generate Getters/Setters.

Upvotes: 12

Related Questions