3

I understand that this question is similar to others asking about Java Properties, and the answers in those questions seem to be "no, you have to use getters/setters".

I searched for C#-like Java properties, but have only found one "hit" so far. And I'm not sure if it is a property or something else.

Reading the Java Tutorial, I came across some text and code that says (** emphasis and comment mine):

Finally, you can use the built-in **length property** to determine the
size of any array. The code:

 //notice lack of brackets after anArray.length :
 System.out.println(anArray.length);

will print the array's size to standard output.

Since Array has a length property, Java does have properties, right?

Can someone point to some documentation about them?

Zabba
  • 64,285
  • 47
  • 179
  • 207

6 Answers6

2

It really depends what definition you're using. Accessing some class's data without using parentheses is a very weak definition of "property." You can make any class/instance member field public and thus have it accessible without using a method.

That doesn't make it like a C# property where you can add custom logic to how getting and setting is done on that field though! You're straight up reading from/writing to a variable. The length field of an array is not writable and is in fact immutable, so it's not horrible style for that to have been done. But other classes that expose their fields have gotten into trouble, like Point and Dimension.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • Ah.. I checked, and `length` is declared as `public final int length`. There's also `getLength()`. I suppose the field is made public because of some convenience factor since the size of an array can't be changed once it exists and so they decided to expose it as a "constant". The Java Tutorial authors should not have used the term "property"! – Zabba Oct 31 '11 at 21:57
  • @Zabba: `getLength()` is not exposed if you have a reference to an array. `Object[] arr...; arr.getLength()` doesn't compile. – Mark Peters Oct 31 '11 at 21:59
  • 1
    @Zabba: Well that tutorial was written before .NET/C# even existed, so I'm not sure you can find them at fault ;-). Not that .NET invented the term. – Mark Peters Oct 31 '11 at 22:00
  • 1
    Maybe :) - but that tutorial has been updated in places for Java 7 too. Also, properties that C# has were existing in Delphi/C++ Builder for a long while before :) And I did see `getLength()` in documentation but did not try compiling. Thanks! – Zabba Oct 31 '11 at 22:02
1

No. There's no documentation about them. Not real properties as in C#, Groovy, etc.

The JavaBean specification is as close as it gets, and that has more to do with satisfying the needs of tooling; it's still just getters, setters, and naming conventions.

"Length" is somewhat anomalous, along with other things about how arrays were implemented.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Anomalous... yep - try explaining to a Java-only programmer how in C++ you can have `map["foo"]` and it's not an array, it's a map. – corsiKa Oct 31 '11 at 21:56
  • @glowcoder: Isn't that more to do with operator overloading than Java's implementation of arrays? :-P – Mark Peters Oct 31 '11 at 21:57
  • @Mark it definitely is. But it's also the only aspect of Java to get that special treatment, hence anomalous. I suppose Strings also received a similar treatment with their + and += operators. – corsiKa Oct 31 '11 at 22:00
1

Java objects can have attributes, which you can think of as a type of property, and they also can have methods. However, "property" in the realm of C# means something specific, which Java does not have.

Of course, Java can mimic them with other features.

Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
1

In Java, public member variables can be considered properties but they are just simple attributes; there is no way to provide getter/setter methods to intercept their reading/writing. For example:

class Foo {
  public String name;
}
Foo f = new Foo();
f.name = "Alice";
f.name; // "Alice"

You can also make them "read-only", as long as you provide a constructor with assignment (or a default value):

class Bar {
  public final String name;
  public Bar(String name) { this.name = name; }
}
Bar b = new Bar("Bob");
b.name; // "Bob"
b.name = "Carol"; // Compile Error: cannot assign to final field.
maerics
  • 151,642
  • 46
  • 269
  • 291
1

No.

Java uses the JavaBeans specification (roughly: getters and setters) to accomplish this.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

Java primitive objects have a handful of things you might regard as "properties", since they are specified as variableName.someName rather than variableName.someName(). length (for an array) is one (and the only one I can think of at present). However, in a bit of sleight-of-hand they are referred to as "fields" in the spec, even though they are not implemented at all like fields.

They are all read-only.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • The other "property" I can think of, even more tenuous than `length`, is `class`, used appended to a class name (eg, `Hashtable.class`) to return the java.lang.Class object for the class name. I'm thinking there may be one or two others, but I'd have to check the Java syntax diagram to find them, I suspect. – Hot Licks Nov 01 '11 at 02:57