Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getter/setter vs properties

 
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
Bartender
Posts: 6589
86
  • 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
Bartender
Posts: 6589
86
  • 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
 
Not so fast naughty spawn! I want you to know about
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!