20

What is the difference between set(String) and setValue(String) in the SimpleStringProperty class?

I know that set(String) is derived from StringPropertyBase, but this makes me even more wonder, why there additionally is setValue(String)?

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • From what I can see, one comes from `WritableValue` and the other from `WritableObjectValue`. Why both interfaces exist is a mystery. Might just be a design flaw. – Joeri Hendrickx Apr 26 '13 at 11:29

2 Answers2

33

set/setValue and get/getValue methods pairs exist to align Object properties with primitive types properties like BooleanProperty or DoubleProperty:

BooleanProperty:

void set(boolean value)
void setValue(java.lang.Boolean v)

DoubleProperty:

void set(double value)
void setValue(java.lang.Number v)

In these property classes ___Value methods work with corresponding to type objects while direct methods work with primitive types.

Looking in the code you may find a bit of a difference in the logic. For example, DoubleProperty#setValue(null) is equal to DoubleProperty#set(0.0) (which was required by binding). So generally I'd advise to use set/get methods and leave setValue/getValue to binding needs as they may incorporate additional logic.

For Object/String properties there is no difference between set and setValue methods.

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • 2
    This is the best explanation you can get, the difference is just a naming convention for the `API` designers. – Salah Eddine Taouririt Apr 26 '13 at 12:34
  • I liked all the answers and they all helped, but this one got the most interesting details for me. Thanks :-) – stefan.at.kotlin Apr 26 '13 at 15:21
  • I think it confusing for me till now. Because setValue() should be for primitive type like other java standard class have. intValue. etc. – Asif Mushtaq Jun 16 '16 at 21:19
  • 1
    Additionally for SimpleStringProperty, the bean may be set at instantiation using the ctor `SimpleStringProperty(Object bean, String name, String initialValue)` and retrieved by using the explicit getter `getBean()`. As stated out, the bean is not used, therefore `SimpleStringProperty(null, "", "")` is a valid call – gkhaos Feb 03 '20 at 17:28
10

StringProperty.java :

@Override
public void setValue(String v) {
    set(v);
}

StringPropertyBase.java:

@Override
public void set(String newValue) {
    if (isBound()) {
        throw new java.lang.RuntimeException("A bound value cannot be set.");
    }
    if ((value == null)? newValue != null : !value.equals(newValue)) {
        value = newValue;
        markInvalid();
    }
}

In common case, you can open sources from open javafx and see that.

Alexander Kirov
  • 3,624
  • 1
  • 19
  • 23
  • 1
    Thanks, also too @tarrsalah, but I yet don't understand what this means? – stefan.at.kotlin Apr 26 '13 at 12:02
  • 1
    "What is the difference between" - there is no difference. One of them call the other. – Alexander Kirov Apr 26 '13 at 12:06
  • 1
    So why does StringProperty add the `setValue()` method? Only for semantic reasons? – stefan.at.kotlin Apr 26 '13 at 12:17
  • 3
    Because of interfaces implementing. System of interfaces is built in a way, so that you need 2 interfaces, which allows you to set value. And not to have a conflict of names, there are different names. One of them set/get come from one interface, and setValue/getValue – Alexander Kirov Apr 26 '13 at 12:40
  • System of interfaces is built in a way, so that you need 72 interfaces, which allows you to have lots of tabs open in your browser, or take a pen and paper and artistically try to make a graph of some sort to make sense in what way it all connects. Please note that the Javadoc "method summary" only explains the direct methods of the class, elevating the sense of joy by letting you click on inline ordered links to interface methods to investigate what the other 90% of its capabilities are. – brat Jun 02 '21 at 08:50