Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6717788

New field level annotation

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 7
    • specification
    • 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 :)

            abuckley Alex Buckley
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: