JDK-6717788 : New field level annotation
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 7
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-06-23
  • Updated: 2011-02-16
  • Resolved: 2008-06-23
Related Reports
Duplicate :  
Description
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 :)