I have been trying to grasp concept of multi-threading and confused about the below code:
class MyClass{ private StringBuilder content = new StringBuilder(); public void setContent(){ content.append("Some String"); content.append("more String"); } public String getContent(){ return content.toString(); } } My understanding is that MyClass cannot be made thread safe just by synchronizing its setter and getter methods. Because while creating MyClass object ,content reference may have improper object initialization. To make proper initialization the content should be final. Can anyone help me to clarify it?
contentshould be final in order to guarantee all threads see its initialized value.