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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With