Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java have "properties" like in C#? [duplicate]

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?

like image 865
Zabba Avatar asked May 24 '26 17:05

Zabba


1 Answers

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.

like image 145
Mark Peters Avatar answered May 27 '26 06:05

Mark Peters