What does "The default initialization of any object happens-before any other actions (other than default- writes) of a program" mean?

StackOverflow https://stackoverflow.com/questions/19121527

  •  30-06-2022
  •  | 
  •  

Question

Java Language Specification 7, section 17.4.5:

5. The default initialization of any object happens-before any other
actions (other than default-writes) of a program.

What does it exactly mean? What is default initialization? Can you provide some examples?

Was it helpful?

Solution 2

Default initialization is process of assigning the instance's fields to their respective default values.

That section you've quoted tells you this happens before, for example, the constructor is called.

OTHER TIPS

Happens-before is a concurrency term. At face value, it means what you think, but it goes beyond that. For non-final, non-volatile fields, Java doesn't guarantee that the field values will by synced across threads unless something establishes a happens-before relationship.

That part of the spec means that other threads accessing the object will see the default initialization values, even if you spawn a thread in the constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top