by Ronald Koster, version 2.0, 2007-07-26
public class Example { property SomeType variable; }Is equivalent to:
public class Example { private SomeType variable; public SomeType getVariable() { return this.variable; } public void setVariable(SomeType variable) { this.variable = variable; } }As can be seen a property has a public getter and setter by default. However, in case an explicitly coded getter or setter is present it overrides the default getter/setter.
public class Example { property SomeType variable; protected SomeType getVariable() { return this.variable; } }
public class Example { property SomeType variable; void setVariable(SomeType variable) { this.variable = variable; } }
NB.1. Of cource when one needs a field not to be visible from outside the class, use an old fashioned private field:
public class Example { private SomeType variable; }
NB.2. For backward compatibility it is perhaps a problem to introduce a new keyword property
.
A possible solution is to name it proprty
instead.
final
modifieer they can be non-private. So the next code should give a syntax error:
public class Example { SomeType variable; // not allowed }But this code is OK:
public class Example { public final SomeType variable; // OK }