One of my main grievances with the Java programming language is it’s lack of Automatic Properties. These have existed in C# since .NET 3.0.
Automatic Properties make property-declaration more concise when no additional logic is required in the property accessors. For example, let’s say we want to implement a simple Book abstraction in an application, with four simple properties for arguments sake.
This is how it may look in C#:
public class Book { public string Name { get; set; } public string Author { get; set; } public string ISBN { get; set; } public string Genre { get; set; } }
Nice and simple, and written in about 1 minute when coupled with Visual Studio’s intellisense etc.
Now, lets take a look at a class offering the same basic functionality in Java:
public class Book { public String Name; public String Author; public String ISBN; public String Genre; public void setName(String name) { Name = name; } public String getName() { return Name; } public void setAuthor(String author) { Author = author; } public String getAuthor() { return Author; } public void setISBN(String isbn) { ISBN = isbn; } public String getISBN() { return ISBN; } public void setGenre(String genre) { Genre = genre; } public String getGenre() { return Genre; } }
Just on a lines of code basis, it’s easy to see that C# wins overall. I understand that the designers of Java may not want to add this functionality to the language due to potentially breaking millions of current Java applications – but I’m sure it could be added in such a way that new applications could use it without breaking legacy applications.
Hell, a third party library, Project Lombok, already provides support for using Automatic Properties in Java, so it’s definitely possible.
I find this limitation really frustrating when working with Java.