-
Enhancement
-
Resolution: Duplicate
-
P5
-
None
-
7
-
x86
-
windows_xp
A DESCRIPTION OF THE REQUEST :
Use annotations to declare private fields as properties. The compiler would detect this and:
1. allow direct assignment to and reading of the field.
2. or if a get/set is defined for the field it would automatically call the get/set.
To the developer using the class there would be no difference. You just use normal operations as if the field was public. Also the compiler would only call a get or set if one is defined, otherwise direct assignment would be used.
[code]
public class Foo {
//no set allowed, only get
@readonlyproperty
private int id;
//can set or get
@property
private float cost;
}
public class Bar() {
public void useFoo(Foo foo) {
int id = foo.id;
float tax = 0.10 * foo.cost;
foo.cost = foo.cost + tax;
}
}
[/code]
Later you could change Foo with no need to change Bar.
[code]
public class Foo {
//no set allowed, only get
@readonlyproperty
private int id;
//can set or get
@property
private float cost;
public float getCost() {
float result = currencyConversionOut(cost);
return result;
}
public void setCost(float cost) {
this.cost = currencyConversionIn(cost);
}
}
[/code]
JUSTIFICATION :
Advantages:
1. Remove overhead of a method when it is not necessary while maintaining encapsulation
2. Allow more natural use of fields in any type of equation
3. No more arguments over public versus private :)
Use annotations to declare private fields as properties. The compiler would detect this and:
1. allow direct assignment to and reading of the field.
2. or if a get/set is defined for the field it would automatically call the get/set.
To the developer using the class there would be no difference. You just use normal operations as if the field was public. Also the compiler would only call a get or set if one is defined, otherwise direct assignment would be used.
[code]
public class Foo {
//no set allowed, only get
@readonlyproperty
private int id;
//can set or get
@property
private float cost;
}
public class Bar() {
public void useFoo(Foo foo) {
int id = foo.id;
float tax = 0.10 * foo.cost;
foo.cost = foo.cost + tax;
}
}
[/code]
Later you could change Foo with no need to change Bar.
[code]
public class Foo {
//no set allowed, only get
@readonlyproperty
private int id;
//can set or get
@property
private float cost;
public float getCost() {
float result = currencyConversionOut(cost);
return result;
}
public void setCost(float cost) {
this.cost = currencyConversionIn(cost);
}
}
[/code]
JUSTIFICATION :
Advantages:
1. Remove overhead of a method when it is not necessary while maintaining encapsulation
2. Allow more natural use of fields in any type of equation
3. No more arguments over public versus private :)
- duplicates
-
JDK-8061409 Explicit (language) support for properties
-
- Closed
-