开发者

Why doesn't Java have automatic properties like C#? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

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 middleNam开发者_开发知识库e;
private String LastName;

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

etc..

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


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


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).


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.


.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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜