-
Enhancement
-
Resolution: Other
-
P4
-
None
-
None
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)
======================================================================
- duplicates
-
JDK-4421126 "Semi" type for variables between private and public.
-
- Closed
-
-
JDK-4857354 RFE: Compiler-generated getters/setters, and calls to getters/setters
-
- Closed
-
-
JDK-6347784 RFE: Add a "property" keyword to avoid the need to invoke getters and setters
-
- Closed
-
-
JDK-6180800 Allow Programmable Fields (C#/VB.NET-style Properties)
-
- Closed
-
-
JDK-6500698 adding a new keyword 'property' to the java language
-
- Closed
-
-
JDK-6668271 Add a property keyword or @Property annotation to the java language
-
- Closed
-
-
JDK-6717788 New field level annotation
-
- Closed
-