-1

What is the difference between these two lines?

return firstName.get();

return firstName;

When should I use one or the other?

Here you have two classes where those lines are used:

package application.model;

import javafx.beans.property.StringProperty;

public class Employee {
    private int id;
    private String firstName,lastName;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }


    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }


    public StringProperty firstNameProperty() {
        return firstName;
    }

}
package ch.makery.address.model;

import java.time.LocalDate;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/**
 * Model class for a Person.
 *
 * @author Marco Jakob
 */
public class Person {

    private final StringProperty firstName;
    private final StringProperty lastName;
    private final StringProperty street;
    private final IntegerProperty postalCode;
    private final StringProperty city;
    private final ObjectProperty<LocalDate> birthday;

    /**
     * Default constructor.
     */
    public Person() {
        this(null, null);
    }

    /**
     * Constructor with some initial data.
     * 
     * @param firstName
     * @param lastName
     */
    public Person(String firstName, String lastName) {
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);

        // Some initial dummy data, just for convenient testing.
        this.street = new SimpleStringProperty("some street");
        this.postalCode = new SimpleIntegerProperty(1234);
        this.city = new SimpleStringProperty("some city");
        this.birthday = new SimpleObjectProperty<LocalDate>(LocalDate.of(1999, 2, 21));
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    public StringProperty firstNameProperty() {
        return firstName;
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String lastName) {
        this.lastName.set(lastName);
    }

    public StringProperty lastNameProperty() {
        return lastName;
    }

    public String getStreet() {
        return street.get();
    }

    public void setStreet(String street) {
        this.street.set(street);
    }

    public StringProperty streetProperty() {
        return street;
    }

    public int getPostalCode() {
        return postalCode.get();
    }

    public void setPostalCode(int postalCode) {
        this.postalCode.set(postalCode);
    }

    public IntegerProperty postalCodeProperty() {
        return postalCode;
    }

    public String getCity() {
        return city.get();
    }

    public void setCity(String city) {
        this.city.set(city);
    }

    public StringProperty cityProperty() {
        return city;
    }

    public LocalDate getBirthday() {
        return birthday.get();
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday.set(birthday);
    }

    public ObjectProperty<LocalDate> birthdayProperty() {
        return birthday;
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
tirenweb
  • 30,963
  • 73
  • 183
  • 303
  • 3
    The type of `firstName` isn't the same between those two examples. In one case it's a `String` and in the other it's a `StringProperty`. – azurefrog Dec 16 '15 at 17:50
  • Aside from the fact that the code you posted doesn't compile, one approach uses the standard "Java Bean" pattern, and one uses the "JavaFX Properties pattern". See http://docs.oracle.com/javase/8/javafx/properties-binding-tutorial/binding.htm#JFXBD107 – James_D Dec 16 '15 at 17:58

1 Answers1

0

In short: It depends on the context.

You may only use .get() when the object has a method get. String objects are very similar to primitives, they are a value and as such can be returned directly (they don't have a get method at all).

Here, StringProperty is a wrapper around (assumedly) a String, meaning if you were to return firstName; from the Person class, you would get a StringProperty instance, not a String - which most likely cannot be used in most operations. So, you return whichever datatype you need.

Note that if you were working with strings in both the source and destination classes, you could just return the value directly, it's already in usable form.

Sam McCreery
  • 542
  • 3
  • 18