> How would I be able to access Foo#a without a being initialized?

Perhaps if a Foo object is being deserialized?  Java serialization doesn't
run constructors.  But that's a degenerate case.

To be clear, the JVM's visibility guarantees for fields only apply when the
instance fields are marked final and thus are set at construction time.
Instance fields marked final are guaranteed to be visible by other Threads
after the constructor completes without the use of synchronization.  Fields
not marked final have no such guarantee.

The bottom line is, in a multithreaded environment, you need either final
fields or synchronized access to those fields.  If you don't use
synchronization, you must use constructors.

-Russ



On Mon, Nov 25, 2013 at 12:24 PM, Cédric Beust ♔ <[email protected]> wrote:

> On Mon, Nov 25, 2013 at 9:09 AM, Brian Pontarelli <[email protected]>
> wrote:
>
>
>> The JVM guarantees that an object and its parent objects are constructed
>> and ready for use after the constructor finishes. It makes no guarantees
>> for fields, especially if they are accessible outside of the object
>> (setters).
>>
> Let’s be specific, here. Assume no setters.
>
> class Foo {
>   private String a;
>
>   public void Foo() {
>     this.a = ...
>   }
>
> How would I be able to access Foo#a without a being initialized?
> --
> Cédric
>
>  --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to