Consider the following models, Apple
public class Apple { private StringProperty appleName = new SimpleStringProperty("Apple"); public String getAppleName() { return appleName.get(); } public StringProperty appleNameProperty() { return appleName; } public void setAppleName(String appleName) { this.appleName.set(appleName); } } and Basket
public class Basket { private Apple apple = new Apple(); public Apple getApple() { return apple; } public void setApple(Apple apple) { this.apple = apple; } } Basket has an apple. Now I'm trying to bind a simple string property as below.
public class Food{ public static void main(String[] args) { StringProperty localApple = new SimpleStringProperty("lGreenApple"); Basket basket = new Basket(); Apple rGreenApple = new Apple(); rGreenApple.setAppleName("rGreenApple"); basket.setApple(rGreenApple); Bindings.bindBidirectional(localApple, rGreenApple.appleNameProperty()); rGreenApple.appleNameProperty().set("rGreenApple 2"); System.out.println(localApple.getValue()); //rGreenApple 2 Apple redApple = new Apple(); redApple.setAppleName("rRedApple"); basket.setApple(redApple); redApple.appleNameProperty().set("rRedApple 2"); System.out.println(localApple.getValue());//Still rGreenApple 2 } } While trying to retrieve value after binding, still localApple object has reference to rGreenApple. What is the clean way to get the red apple?