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