views:

287

answers:

6

C# has automatic properties which greatly simplify your code:

public string Name { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }

Whereas Java has you write this much code:

private String name;
private String middleName;
private String LastName;

public String Name(){
   return this.name;
}

etc..

Is there a particular reason Java hasn't implemented something like this?

+13  A: 

Yep, because it doesn't have it. As the saying goes, all features start out unimplemented.

Noon Silk
@finnw: I completely agree the voting strategy here is broken, but my answer is correct. Jespers answer is more detailed, but he posted it later. Take your hate to the meta forum.
Noon Silk
It's not hate, it's justification for my downvote. I've said plenty about this on meta already.
finnw
@finnw: Fair enough, I appreciate the justifcation but don't care to be involved in any meta argument regarding this site.
Noon Silk
+1  A: 

History. Java was designed and implemented a number of years before C#, and the designers did not have the benefit of hindsight. Once implemented, it was hard to add new features without creating backwards compatibility problems.

Seriously, if not having C# properties in Java is a real problem for you, consider using a different programming language.

Stephen C
Why the hostility towards the ending of your answer? I asked this questions because it seems that Java and C# are like cousins.
Sergio Tapia
IMO, C# is what Java should have been. But really, they're cousins in about the same way that C++ and Java are cousins -- which is to say, not really. They just happen to look a lot alike.
cHao
@Sergio - I guess I'm just reacting to the implicit criticism in your question.
Stephen C
@cHao - another interpretation is that C# is what happened after Microsoft got spanked by the US courts for violating the terms of their java license. If that hadn't happened, Microsoft would still be pushing their incompatible variant of Java, locking Java users into their platforms.
Stephen C
Stephen C: another interpretation is that MS is still pushing their incompatible variant of Java, locking Java into their platforms -- only they call it C# now because of the lawsuit.
Gabe
And yet another interpretation is that they took their broken Java, added COM interop capability, all-around spiffed the hell out of it, and had to give it a new name cause it wasn't even nearly the same language anymore.
cHao
cHao: Microsoft's implementation of Java had COM interop even without their "unholy" extensions. Indeed, I would say that J++ was more like their "fixed" Java rather than broken Java, as some features they added are only now making it into the language.
Gabe
+2  A: 

.net came along after Java, and one of its goals was to interoperate with COM. COM had properties, probably a la VB, so .net all but had to add them in order to make languages interoperable. (That, and they were a pretty spiffy idea.)

Java didn't have such a need, and its creators probably didn't want to pollute the meaning of "=" or make function calls look like member variables. (They are -- or at least were, at some point -- big on keeping the language as clean as possible.) Their approach was instead the Java bean specs, which specified a naming convention for getters and setters. IDEs that know the spec can more-or-less fudge the view of the getter and setter as a single "property" for design purposes.

cHao
+4  A: 

It is not so easy to add new features to an existing programming language, especially if you care about backward compatibility. Sun has always been extremely careful with adding new features to Java, because they wanted to be absolutely sure that any new language feature will not break the millions of Java programs that have been written over the years.

So, it's not just a question of adding this to the language; you have to think very carefully, and try it out, to discover if there aren't any subtle backward compatibility problems with whatever new feature you want to add.

There have been proposals to add support for properties in one form or another in Java, but it looks like for Java 7 (the next version coming up) this is not a feature that is being considered.

You might want to have a look at Project Lombok, which is a kind of extension to Java, using annotations, to make it possible to write more concise code (it can, for example, automatically generate getters and setters for fields).

Jesper
+1 Lomboks @Data also adds toString,equals and hashcode
stacker
+1  A: 

For same reason the C# 2.0 or lower does't have this. It's more or syntactical sugar rather a language feature. There are ton of features like this which could be added to any language, but not one knows why they are not the language.

Ramesh Soni
+1  A: 

Automatic properties are built upon properties.

Properties in C# are a language feature, in Java they are a convention (methods starting with get or set are often considered properties by people talking about the code, but to the compiler it's no different than foo or bar).

.NET and its associated languages where in many ways based upon COM (sometimes in following suit, sometimes in deliberately not doing something in COM that was for some reason or other unpopular).

COM has a concept of properties which in VB was backed by language features (in C++ it was backed by conventions).

Early versions of VB, especially in contexts where it was used to provide basic programmatic access to object models provided from elsewhere, aimed to simplify the object models presented to users to make the distinction between a field and a method that gets or sets (perhaps with additional work, perhaps not) unimportant (consider that while they differ in some important way from the outside in .NET, syntactically accessing a property and a public field is the same). When VB and COM (and before that OLE) grew up, they grew up together.

So in all, while C# shares a C/C++ inheritance with Java, it also has an inheritance that Java does not share, which made properties seem like a good idea to C#'s creators, but not to Java's.

Edit: At first I said:


Personally, I think automatic properties are really a workaround to a flaw in properties, rather than a way to simplify your code. Properties "look" like public fields syntactically, but aren't entirely (try using DataBinder.Eval to retrieve a field). If a property was both fully public, and had no additional functionality in getter or setter (which is the case with automatic properties), I'd rather just have a public field (encapsulation is no argument here, as fully exposing it publicly by-passes that anyway), but the differences between fields and properties works against that.


I retract that statement. Making fields exactly like properties would require the fields of simple Plain-Old-Data structs to act like properties, which would be a performance loose. Conceptually, I'd like them to be more like each other, but whenever I think on it (and I've gone from being annoyed to going "oh wait" like here more than once), it's better as it is.

Jon Hanna