WhyJava Bean Pattern is not threadsafe

Viewed 692

Joshua Bloch states in Effective Java, 2nd Edition:

One alternative you have to the Telescoping Constructor Pattern is the JavaBean Pattern where you call a constructor with the mandatory parameters and then call any optional setters after:

Pizza pizza = new Pizza(12);
pizza.setCheese(true);
pizza.setPepperoni(true);
pizza.setBacon(true);

The problem here is that because the object is created over several calls it may be in an inconsistent state partway through its construction. This also requires a lot of extra effort to ensure thread safety.

My Question:- Is above code not thread safe? Am I missing any basic thing?

Thanks in Advance,

Surya

2 Answers
Related