Java Reference
In-Depth Information
public synchronized void setQuantityOrdered(int
quantityOrdered) {
this.quantityOrdered = quantityOrdered;
}
public synchronized void setCustomerName(String
customerName) {
this.customerName = customerName;
}
}
Solution 2
Create an immutable object (an object that, once created, doesn't change its internal
state). In the following code, the internal variables of the object are declared final
and are assigned at construction. By doing so, it is guaranteed that the object is immut-
able:
class ImmutableCustomerOrder {
final private String itemOrdered;
final private int quantityOrdered;
final private String customerName;
ImmutableCustomerOrder(String itemOrdered, int
quantityOrdered, String customerName) {
this.itemOrdered = itemOrdered;
this.quantityOrdered = quantityOrdered;
this.customerName = customerName;
}
public String getItemOrdered() {
return itemOrdered;
}
public int getQuantityOrdered() {
return quantityOrdered;
}
Search WWH ::




Custom Search