devoured elysium
devoured elysium

Reputation: 105067

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

Upvotes: 16

Views: 4958

Answers (5)

jwenting
jwenting

Reputation: 5663

C# properties are similar to Delphi properties for the simple reason that they were inspired by Delphi properties. Having one of the key people from Borland's Delphi team move to Microsoft and (help) design C# had of course nothing whatsoever to do with that :)

There was a proposal (several in fact) last year for adding C# style properties to Java, but those were effectively shot down by the community as being mere syntactic sugar with no real added value.

Upvotes: 0

stiank81
stiank81

Reputation: 25686

No

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

Upvotes: 18

Bryce Fischer
Bryce Fischer

Reputation: 5442

As the others said, no Java does not have properties per se. The accepted method is to use getters and setters. On a side note, Delphi has support for properties that is very similar to C#'s.

Definitely nothing like AutoProperties is supported in Java. Can't seem to find any mention of any future support for it either.

Upvotes: 4

chama
chama

Reputation: 6163

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.

Upvotes: 4

brian
brian

Reputation: 1080

Last time I checked, Java did not support properties.

Upvotes: 2

Related Questions