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
  •  | 
  •  

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?

有帮助吗?

解决方案 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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top