Reputation: 107
I read the following statement in Java Concurrency in Practice by Brian Goetz, 2006 edition.
"While it is possible to write a thread safe program that stores all its state in public static fields,it is a lot harder to verify its thread safety..." (Chapter4 - Composing Objects; 1st paragraph of 4.1 Designing a Thread Safe Class)
All this while I thought that static fields belong to a class and can't store an object's state. Am I misinterpreting something?
Upvotes: 0
Views: 192
Reputation: 14015
Author said nothing about static fields belong to object. He meant that program, that stores data in static fields of classes (of cause), is much harder make it really thread safe, than program, "... that uses encapsulation appropriately."
And yes, instances(objects) are able store data in static fields of class. But author had in mind another thing.
Upvotes: 2