java123999 java123999 - 5 months ago 7
Java Question

Setting a String field of an entity using a boolean?

The following is an

Entity
named Person:

@Entity
@Table(
name = "PERSON"
)
public class Person {

//other fields etc

@Column(
name = "PAID_IN_FULL",
nullable = false
)
private String paidInFull;

public void setPaidInFull(boolean paidInFull) {
this.paidInFull = paidInFull?"Y":"N";
}

}


Using the setter above, if I were to do the following:

person.setPaidInFull(true);


What string value would that set it to, would it be
"Y"
or
"N"
?

Answer
paidInFull?"Y":"N";

Is another way if saying:

if (paidInFull)
    return "Y";
else
    return "N";

So if you were to do

person.setPaidInFull(true);

this.paidInFull would be "Y".

Here is a complete example:

package nl.testing.startingpoint;

public class Main {
    private static String paidInFull;

    public static void main(String args[])
    {
        setPaidInFull(true);
        System.out.println(paidInFull);
    }

    public static void setPaidInFull(boolean paidInFull) {
        Main.paidInFull = (paidInFull) ? "Y" : "N";
    }
}