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()?
Share
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 :
You might want to have a look at Project Lombok which provides the annotations
@Getterand@Setterthat are somewhat similar to Python’sproperty.With Lombok, the above example reduces to :