Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absence of property syntax in Java

C# has syntax for declaring and using properties. For example, one can declare a simple property, like this:

public int Size { get; set; }

One can also put a bit of logic into the property, like this:

public string SizeHex
{
    get
    {
        return String.Format("{0:X}", Size);
    }
    set
    {
        Size = int.Parse(value, NumberStyles.HexNumber);
    }
}

Regardless of whether it has logic or not, a property is used in the same way as a field:

int fileSize = myFile.Size;

I'm no stranger to either Java or C# -- I've used both quite a lot and I've always missed having property syntax in Java. I've read in this question that "it's highly unlikely that property support will be added in Java 7 or perhaps ever", but frankly I find it too much work to dig around in discussions, forums, blogs, comments and JSRs to find out why.

So my question is: can anyone sum up why Java isn't likely to get property syntax?

  • Is it because it's not deemed important enough when compared to other possible improvements?
  • Are there technical (e.g. JVM-related) limitations?
  • Is it a matter of politics? (e.g. "I've been coding in Java for 50 years now and I say we don't need no steenkin' properties!")
  • Is it a case of bikeshedding?
like image 735
Vojislav Stojkovic Avatar asked Feb 03 '09 20:02

Vojislav Stojkovic


2 Answers

I think it's just Java's general philosophy towards things. Properties are somewhat "magical", and Java's philosophy is to keep the core language as simple as possible and avoid magic like the plague. This enables Java to be a lingua franca that can be understood by just about any programmer. It also makes it very easy to reason about what an arbitrary isolated piece of code is doing, and enables better tool support. The downside is that it makes the language more verbose and less expressive. This is not necessarily the right way or the wrong way to design a language, it's just a tradeoff.

like image 77
dsimcha Avatar answered Sep 28 '22 08:09

dsimcha


For 10 years or so, sun has resisted any significant changes to the language as hard as they could. In the same period C# has been trough a riveting development, adding a host of new cool features with every release.

I think the train left on properties in java a long time ago, they would have been nice, but we have the java-bean specification. Adding properties now would just make the language even more confusing. While the javabean specification IMO is nowhere near as good, it'll have to do. And in the grander scheme of things I think properties are not really that relevant. The bloat in java code is caused by other things than getters and setters.

There are far more important things to focus on, such as getting a decent closure standard.

like image 26
krosenvold Avatar answered Sep 28 '22 08:09

krosenvold