views:

315

answers:

6

I'm new to Java, and I'd like to create some class variables that are dynamically calculated when accessed, as you can do in Python by using the property() method. However, I'm not really sure how to describe this, so Googling shows me lots about the Java "Property" class, but this doesn't appear to be the same thing. What is the Java equivalent of Python's property()?

A: 

Do you want to create new fields/getters/setters in the class? If you want to do this in runtime, you have to create completely new class with your fields and methods, and load it into the JVM. To create new class you can use library like ASM or CGLib, but if you're new to Java, this isn't something you want to start with.

Peter Štibraný
+6  A: 

There's no such facility built into Java language. You have to write all the getters and setters explicitly by yourself. IDEs like Eclipse can generate this boilerplate code for you though.

For example :

class Point{
  private int x, y;

  public Point(int x, int y){
    this.x = x;
    this.y = y;
  }

  public void setX(int x){
    this.x = x;
  }

  public int getX(){
    return x;
  }

  public void setY(int y){
    this.y = y;
  }

  public int getY(){
    return y;
  }
}

You might want to have a look at Project Lombok which provides the annotations @Getter and @Setter that are somewhat similar to Python's property.

With Lombok, the above example reduces to :

class Point{
  @Getter @Setter private int x, y;

  public Point(int x, int y){
    this.x = x;
    this.y = y;
  }
}
missingfaktor
+1  A: 

They don't really exist. In Java it's common practice to declare members as private or protected and only allow access to them via methods. Often this leads to lots of small getFoo() and setFoo(newFoo) methods. Python doesn't really have private and protected and it's more common to allow direct access to members.

Tom Dunham
+1  A: 

As others have noted, java has getters and setters, but there is no strict analogon. There is a third party library called Project Lombok tha uses annotation to generate the getters and setters in the .class files at comile time. This could be used to make things a little less verbose.

mR_fr0g
-1, When you have nothing new to add, you should upvote the already existing answers.
Red Hyena
My addition was pointing out Project Lombok. GreyMatter 9.0 then later edited his answer to include it.
mR_fr0g
@mR_fr0g: Don't make silly accusations. The edit time of my answer (13:18) is before the posting time of your answer (13:33).
missingfaktor
A: 

Actually you may simulate this behavior in Java.

WARNING: ugly solution below

You can write a method in an utility class like the code below:

public Object getProperty(String property, Object obj) {
  if (obj != null && property != null) {
       Field field = obj.getClass().getDeclaredField(property);
       field.setAccessible(true);
       try {
          return field.get(obj);
       } catch (Exception ex) {
          return null;
       }
  }
  return null;
}

You may actually declare it as a static method and then just import this method into your classes which will need this behavior.

By the way, Groovy support this feature.

Kico Lobo
Please read my post more closely. I wasn't looking for a function to access instance members. I was looking for a feature that calls a function when I reference an instance member.e.g.myobj.attribute = 123;would actually do:myobj.setAttribute(123);
Chris S
+2  A: 

the built-in property() function does exactly the opposite of what's described here in the answers. it's not about generating getters and setters for member variables. it just allows you to call a method by accessing a property (so, although you just access a variable in te Python class, a function will be called). (this post ecplains how and why to use it.)
this said, Java doesn't offer anything like this. even more, property access is discouraged in Java. i guess you could do it in Groovy script language and the meta magic though. but i don't know out of my head how to do this.

Stefan De Boey