Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java and C#-like properties

Does Java natively support properties, like C#? Or when coding in Java, when trying to encapsulate variables, you are constrained to do it by getVariable() and setVariable() kinda methods?

Thanks

like image 849
devoured elysium Avatar asked Feb 22 '10 17:02

devoured elysium


2 Answers

No

You don't have the concept of Properties in the Java language. You need to use getters and setters..

like image 105
stiank81 Avatar answered Oct 22 '22 23:10

stiank81


Java does not support C# like properties. You need getters and setters, but be careful that you don't accidentally give your user access to the encapsulated data by returning a reference to the object instead of a reference to a copy of it.

like image 28
chama Avatar answered Oct 22 '22 23:10

chama