Init'ing fields

Discussion in 'Java' started by James D Carroll, Jan 4, 2005.

  1. I've code of both varieties below:

    public class foo {
    someClass sc;

    public foo (){
    sc = new someClass(): //init member in ctor
    }
    }

    ** versus **

    public class foo() {
    someClass sc = new someClass; /init member outside ctor

    public foo(){
    }
    }

    Is there any real difference between the two?


    Thanks,
     
    James D Carroll, Jan 4, 2005
    #1
    1. Advertising

  2. James D Carroll

    Tom Dyess Guest

    Yes and no, the latter is a shortcut to the former. It DOES run the
    constructor code before the inline declaration/creation objects definitions
    though. Some consider this bac practice because you are having objects
    constructed outside the constructor. I do it though, but I do have to be
    mindful of them when otherwise, I would just have to be mindful of the
    constructor.

    "James D Carroll" <> wrote in message
    news:...
    > I've code of both varieties below:
    >
    > public class foo {
    > someClass sc;
    >
    > public foo (){
    > sc = new someClass(): //init member in ctor
    > }
    > }
    >
    > ** versus **
    >
    > public class foo() {
    > someClass sc = new someClass; /init member outside ctor
    >
    > public foo(){
    > }
    > }
    >
    > Is there any real difference between the two?
    >
    >
    > Thanks,
    >
    >
    >
     
    Tom Dyess, Jan 4, 2005
    #2
    1. Advertising

  3. James D Carroll

    Tony Morris Guest

    "James D Carroll" <> wrote in message
    news:...
    > I've code of both varieties below:
    >
    > public class foo {
    > someClass sc;
    >
    > public foo (){
    > sc = new someClass(): //init member in ctor
    > }
    > }
    >
    > ** versus **
    >
    > public class foo() {
    > someClass sc = new someClass; /init member outside ctor
    >
    > public foo(){
    > }
    > }
    >
    > Is there any real difference between the two?
    >
    >
    > Thanks,
    >
    >
    >


    Yes there is a difference.
    Some consider the latter poor form, but only to avoid the consequences of
    writing code that is truly poor, specifically, when you call an overridable
    method on a this (implicit or not) reference from a constructor, there may
    some side effect and you should *NEVER* do that.

    Here's an example:
    http://www.xdweb.net/~dibblego/java/trivia/answers.html#q4


    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Jan 4, 2005
    #3
  4. James D Carroll

    Ryan Stewart Guest

    "Tony Morris" <> wrote in message
    news:UjnCd.102534$...
    > Some consider the latter poor form, but only to avoid the consequences of
    > writing code that is truly poor, specifically, when you call an
    > overridable
    > method on a this (implicit or not) reference from a constructor, there may
    > some side effect and you should *NEVER* do that.
    >

    I thought we had this discussion here already. "Never" is a strong word.

    > Here's an example:
    > http://www.xdweb.net/~dibblego/java/trivia/answers.html#q4
    >

    You should really use clearer terms on this site. Wouldn't you consider an
    object to be created when memory is allocated for it and its members are
    initialized? Besides which, initialization is a part of object instantiation
    (two parts, actually). The two are not separate processes.
     
    Ryan Stewart, Jan 4, 2005
    #4
  5. James D Carroll

    Guest

    I have run into problems using the latter when subclassing, because of
    the execution order.
     
    , Jan 4, 2005
    #5
  6. Thanks all.



    "James D Carroll" <> wrote in message
    news:...
    > I've code of both varieties below:
    >
    > public class foo {
    > someClass sc;
    >
    > public foo (){
    > sc = new someClass(): //init member in ctor
    > }
    > }
    >
    > ** versus **
    >
    > public class foo() {
    > someClass sc = new someClass; /init member outside ctor
    >
    > public foo(){
    > }
    > }
    >
    > Is there any real difference between the two?
    >
    >
    > Thanks,
    >
    >
    >
     
    James D Carroll, Jan 4, 2005
    #6
  7. James D Carroll

    Tony Morris Guest

    "Ryan Stewart" <> wrote in message
    news:...
    > "Tony Morris" <> wrote in message
    > news:UjnCd.102534$...
    > > Some consider the latter poor form, but only to avoid the consequences

    of
    > > writing code that is truly poor, specifically, when you call an
    > > overridable
    > > method on a this (implicit or not) reference from a constructor, there

    may
    > > some side effect and you should *NEVER* do that.
    > >

    > I thought we had this discussion here already. "Never" is a strong word.


    We did?
    I meant to use a strong word. It should never occur.
    Some programming languages go on to prevent it at compile-time.
    Java unfortunately, doesn't.

    > > Here's an example:
    > > http://www.xdweb.net/~dibblego/java/trivia/answers.html#q4
    > >

    > You should really use clearer terms on this site. Wouldn't you consider an
    > object to be created when memory is allocated for it and its members are
    > initialized? Besides which, initialization is a part of object

    instantiation
    > (two parts, actually). The two are not separate processes.
    >
    >


    I used the terminology from the Java Language Specification Second Edition
    when I wrote it.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Jan 4, 2005
    #7
  8. Tom Dyess wrote:

    > Yes and no, the latter is a shortcut to the former. It DOES run the
    > constructor code before the inline declaration/creation objects definitions
    > though.


    Careful. Variable initializers and instance initializers are run after
    the *superclass'* constructor but before the (rest of) the constructor
    body for the class in which they appear. This is as specified in
    JLS(2e) 8.8.6. It is easy to write a test class that demonstrates this
    behavior:


    public class InitOrder {
    int initWithInitializer = 42;
    int initInConstructor;

    public InitOrder() {
    /*
    * Implicit invocation of Object() occurs here
    *
    * The instance variable initializer then runs before the
    * (rest of) the constructor body.
    */

    System.out.println("At constructor start:");
    System.out.println(" initWithInitializer = "
    + initWithInitializer);
    System.out.println(" initInConstructor = "
    + initInConstructor);

    /*
    * The last println() above shows initInConstructor with its
    * default value
    */

    initInConstructor = 17;

    System.out.println("At constructor end:");
    System.out.println(" initWithInitializer = "
    + initWithInitializer);
    System.out.println(" initInConstructor = "
    + initInConstructor);
    }

    public static void main(String[] args) {
    InitOrder instance = new InitOrder();

    System.out.println("After constructor:");
    System.out.println(" initWithInitializer = "
    + instance.initWithInitializer);
    System.out.println(" initInConstructor = "
    + instance.initInConstructor);
    }
    }


    ====

    D:\temp\testdir>java -cp . InitOrder
    At constructor start:
    initWithInitializer = 42
    initInConstructor = 0
    At constructor end:
    initWithInitializer = 42
    initInConstructor = 17
    After constructor:
    initWithInitializer = 42
    initInConstructor = 17

    ====


    John Bollinger
     
    John C. Bollinger, Jan 4, 2005
    #8
  9. James D Carroll

    Chris Smith Guest

    Tony Morris <> wrote:
    > Yes there is a difference.
    > Some consider the latter poor form, but only to avoid the consequences of
    > writing code that is truly poor, specifically, when you call an overridable
    > method on a this (implicit or not) reference from a constructor, there may
    > some side effect and you should *NEVER* do that.


    How is this relevant to calling such a method from a superclass
    constructor? In either of the code samples given, the field would not
    have been initialized yet when the method is called.

    > Here's an example:
    > http://www.xdweb.net/~dibblego/java/trivia/answers.html#q4


    That's an example of something, but I'd like to hear how you think it's
    relevant to this thread. I might be missing something here.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Jan 4, 2005
    #9
  10. James D Carroll

    Ryan Stewart Guest

    "Tony Morris" <> wrote in message
    news:VlpCd.102611$...
    > I meant to use a strong word. It should never occur.
    >

    Never say never.

    > Some programming languages go on to prevent it at compile-time.
    > Java unfortunately, doesn't.
    >

    Personally, I'm glad to be using a language that doesn't tell me I can't do
    something just because I might not understand what I'm doing.

    > I used the terminology from the Java Language Specification Second Edition
    > when I wrote it.
    >

    Java Language Specification 2.0 ยง12.5:
    "Whenever a new class instance is created, memory space is allocated for
    it..."
    1) Class instantiation/object creation begins

    "[and] all the instance variables in the new object, including those
    declared in superclasses, are initialized to their default values."
    2) First initialization process

    "Just before a reference to the newly created object is returned as the
    result, the indicated constructor is processed to initialize the new object
    using the following procedure..."
    3) Second initialization process and end of class instantiation/object
    creation
     
    Ryan Stewart, Jan 4, 2005
    #10
  11. James D Carroll

    Tom Dyess Guest

    Whoops. Good eye.

    "John C. Bollinger" <> wrote in message
    news:cre9ga$nli$...
    > Tom Dyess wrote:
    >
    >> Yes and no, the latter is a shortcut to the former. It DOES run the
    >> constructor code before the inline declaration/creation objects
    >> definitions though.

    >
    > Careful. Variable initializers and instance initializers are run after
    > the *superclass'* constructor but before the (rest of) the constructor
    > body for the class in which they appear. This is as specified in JLS(2e)
    > 8.8.6. It is easy to write a test class that demonstrates this behavior:
    >
    >
    > public class InitOrder {
    > int initWithInitializer = 42;
    > int initInConstructor;
    >
    > public InitOrder() {
    > /*
    > * Implicit invocation of Object() occurs here
    > *
    > * The instance variable initializer then runs before the
    > * (rest of) the constructor body.
    > */
    >
    > System.out.println("At constructor start:");
    > System.out.println(" initWithInitializer = "
    > + initWithInitializer);
    > System.out.println(" initInConstructor = "
    > + initInConstructor);
    >
    > /*
    > * The last println() above shows initInConstructor with its
    > * default value
    > */
    >
    > initInConstructor = 17;
    >
    > System.out.println("At constructor end:");
    > System.out.println(" initWithInitializer = "
    > + initWithInitializer);
    > System.out.println(" initInConstructor = "
    > + initInConstructor);
    > }
    >
    > public static void main(String[] args) {
    > InitOrder instance = new InitOrder();
    >
    > System.out.println("After constructor:");
    > System.out.println(" initWithInitializer = "
    > + instance.initWithInitializer);
    > System.out.println(" initInConstructor = "
    > + instance.initInConstructor);
    > }
    > }
    >
    >
    > ====
    >
    > D:\temp\testdir>java -cp . InitOrder
    > At constructor start:
    > initWithInitializer = 42
    > initInConstructor = 0
    > At constructor end:
    > initWithInitializer = 42
    > initInConstructor = 17
    > After constructor:
    > initWithInitializer = 42
    > initInConstructor = 17
    >
    > ====
    >
    >
    > John Bollinger
    >
     
    Tom Dyess, Jan 4, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Cheng
    Replies:
    1
    Views:
    8,318
    Juan T. Llibre
    Feb 24, 2006
  2. Replies:
    1
    Views:
    696
    Jules
    Aug 18, 2005
  3. Jess
    Replies:
    4
    Views:
    470
  4. news.aon.at
    Replies:
    11
    Views:
    678
    Ian Collins
    Jan 29, 2011
  5. Aamer Akhter

    tcltklib and not init'ing tk

    Aamer Akhter, Oct 1, 2003, in forum: Ruby
    Replies:
    8
    Views:
    145
    Hidetoshi NAGAI
    Oct 16, 2003
Loading...

Share This Page