JDK-8061409 : Explicit (language) support for properties
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Priority: P4
  • Status: Closed
  • Resolution: Other
  • Submitted: 1999-04-11
  • Updated: 2014-10-17
  • Resolved: 2014-10-17
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Duplicate :  
Description
Name: krT82822			Date: 04/11/99


Java does not explicitly support properties currently.

To simulate properties, the Java API uses the 
setProperty()/getProperty() methods. 

It would be nice if properties were part of the language.

Current state of Java without properties:

class XYZ
{

   // property color
   private Color color;

   public Color getColor()
   { return color; }

   public void setColor(Color newColor)
   { color = newColor; }   
}

...

// client code
void mymethod(XYZ obj)
{
   // use component
   Color c;

   // we have to use get/set methods
   c = obj.getColor();
   obj.setColor(Color.red);
}

As you can see using get/set methods is okay. But in my
opinion, it is not very readable. 

Let's say Java supported properties. We might then have
a new keyword 'property' to indicate a property.
Here is a proposed solution:

class XYZ
{
   // property color

   // declare a property color of type Color  
   public property Color color;  

   // define a method for setting the color
   public get property Color color()
   { return color; }

   public set property void color(Color newColor)
   { color = newColor; }

}

The above definition of properties might seem more work at
first. But it is clear in its intent - that color is a 
properties. The use of get/set methods is a hack.
Furthermore, using the properties is very readable.

void mymethod(XYZ obj)
{
   // use component
   Color c;

   // Before, after scenarios for properties   

   // get the color
   c = obj.getColor();     // before
   c = obj.color;          // after

   // assign a new color
   obj.setColor(Color.red);  // before
   obj.color = Color.red;    // after
}

This feature might not seem much. But I'm sure that if such
a feature existed in Java, properties would be used more than
they are now.
I am of the opinion that properties are part of the
general Java language and not just to be used by JavaBeans.
This syntactic-sugar might make Java more readable.
(Review ID: 56803) 
======================================================================

Comments
The 'specification' component of the Java Bug System is for reporting technical errors and ambiguities in the text of The Java Language Specification and The JVM Specification. It is not the venue to propose new features in the Java language or JVM. Ongoing feature development is carried out in OpenJDK (http://openjdk.java.net/jeps/); corresponding enhancements to The Java Language Specification and The JVM Specification are managed through the Java Community Process (http://jcp.org/).
17-10-2014

EVALUATION In principle, some form of this is a good idea. The main benefit would be the enforced encapsulation of representation: properties (or attributes, or whatever you choose to call them) would automatically introduce access methods. They would be a shorthand for defining a field with a get and set method. The further sugar of making the accesses look like field accesses is actually damaging. It makes it impossible to replace a property implemented by a field with a pair of methods. In practice, I don't expect such sugar to be added to Java. It adds no real power, and without corresponding restrictions to enforce its use (i.e., make all fields private, which we cannot do) it is fairly inconsequential. gilad.bracha@eng 1999-04-15
15-04-1999