On Nov 25, 2013, at 10:24 AM, 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? > > You can’t. You also can’t set “a” outside of “Foo" without reflection unless you pass a value or an object that retrieves the value into the constructor. If your object uses “a” and never assigns it, it is difficult to understand where “a” is coming from. This is also difficult to test. My point was that fields are treated differently than constructors. Java makes no guarantees about fields but it does make guarantees about constructors. That’s why I pass dependencies into the constructor and don’t rely on some external system (some call it magic) to set my fields. — Brian -- 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.
