So, I have willfully kept myself a Java n00b until recently, and my first real exposure brought about a minor shock: Java does not have C# style properties!
Ok, I can live with that. However, I can also swear that I have seen property getter/setter code in Java in one codebase, but I cannot remember where. How was that achieved? Is there a language extension for that? Is it related to NetBeans or something?
Realistically however, we have to accept that in all likelihood Java will never have properties as a language feature.
Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key is a String and the value is also a String. The Properties class is used by many other Java classes. For example, it is the type of object returned by System. getProperties( ) when obtaining environmental values.
Java is an object-oriented programming language, which views the world as a collection of objects that have both properties and behavior.
Properties files are mostly used to store configuration or localization data. They are used to externalize the data which is configurable to the application. If you put such data in your code (as a enum or class ) and want to modify it, you have to build the code again.
There is a "standard" pattern for getters and setters in Java, called Bean properties. Basically any method starting with get
, taking no arguments and returning a value, is a property getter for a property named as the rest of the method name (with a lowercased start letter). Likewise set
creates a setter of a void method with a single argument.
For example:
// Getter for "awesomeString" public String getAwesomeString() { return awesomeString; } // Setter for "awesomeString" public void setAwesomeString( String awesomeString ) { this.awesomeString = awesomeString; }
Most Java IDEs will generate these methods for you if you ask them (in Eclipse it's as simple as moving the cursor to a field and hitting Ctrl-1, then selecting the option from the list).
For what it's worth, for readability you can actually use is
and has
in place of get
for boolean-type properties too, as in:
public boolean isAwesome(); public boolean hasAwesomeStuff();
I am surprised that no one mentioned project lombok
Yes, currently there are no properties in java. There are some other missing features as well.
But luckily we have project lombok that is trying to improve the situation. It is also getting more and more popular every day.
So, if you're using lombok:
@Getter @Setter int awesomeInteger = 5;
This code is going to generate getAwesomeInteger
and setAwesomeInteger
as well. So it is quite similar to C# auto-implemented properties.
You can get more info about lombok getters and setters here.
You should definitely check out other features as well. My favorites are:
Lombok is well-integrated with IDEs, so it is going to show generated methods like if they existed (suggestions, class contents, go to declaration and refactoring).
The only problem with lombok is that other programmers might not know about it. You can always delombok the code but that is rather a workaround than a solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With