Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getter/setter vs properties  RSS feed

 
Jack Kenshin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While studying JavaFX, I have seen a new feature of Java 8 that properties. I have researched as well that its usage is common in C#. So, the point I wonder shouldn't I use getter/setter anymore because of properties? However, can you tell the fundamental difference between them? (why Java developers have added the feature)
 
Stephan van Hulst
Saloon Keeper
Posts: 6966
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java doesn't have object properties. Do you have an example of what you are referring to?
 
Jack Kenshin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Java doesn't have object properties. Do you have an example of what you are referring to?





 
Stephan van Hulst
Saloon Keeper
Posts: 6966
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a patterns that is used by JavaFX, but it's not actually the same as real object properties in languages such as C#. In C#, you can actually treat properties as if you're accessing a field, and the underlying getters and setters are automatically called. This is not the case in Java.

I'm not quite sure yet why JavaFX uses this pattern, I'm not very familiar with JavaFX.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I'm not quite sure yet why JavaFX uses this pattern, I'm not very familiar with JavaFX.

I think it's to allow access to a property by "name"; but the API looks awfully clumsy.

@Jack: Java 8 has an Optional class, which is a bit like a read-only 'property', although that's not why it was created and it doesn't connect a property with a name.
Its API might give you some ideas for setting up your own Property class though.

And if you decide to, I suggest you read the JavaBeans specification, as you might want to have it support the PropertyChangeSupport framework.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!