Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(no) Properties in Java?

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?

like image 437
Ishmaeel Avatar asked Sep 16 '08 08:09

Ishmaeel


People also ask

Will Java ever get properties?

Realistically however, we have to accept that in all likelihood Java will never have properties as a language feature.

What is meant by properties in Java?

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.

Do Java objects have properties?

Java is an object-oriented programming language, which views the world as a collection of objects that have both properties and behavior.

Why do we use properties in Java?

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.


2 Answers

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(); 
like image 117
Calum Avatar answered Sep 21 '22 15:09

Calum


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:

  • val
  • NoArgsConstructor, RequiredArgsConstructor, AllArgsConstructor
  • Logs!

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.

like image 33
Aleks-Daniel Jakimenko-A. Avatar answered Sep 22 '22 15:09

Aleks-Daniel Jakimenko-A.