Results 1 to 5 of 5

Thread: Initialization

  1. #1
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default Initialization

    Hi,

    I'm not new to Java, but I thought it wise to post it here. It might classify as beginner question, but it is bugging me for some time now, and is difficult to find the answer in books or on the web.

    Does anybody know the difference between (fictive example):

    1)
    Java Code:
    public class Test {
        private SomeClass sc = new SomeClass();
    }
    and

    2)
    Java Code:
    public class Test {
        private SomeClass sc;
    
        public Test() {
             sc = new SomeClass();
        }
    My guess is that they do *exactly* the same thing, i.e. initializing the sc variable at the time new is invoked.

    Is there any semantic difference between the two alternatives and if so, what exactly?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no pro here either, but that won't stop me from putting in my 2 cents. For me I think that the two statements above do the same thing, but since your class can have multiple constructors, if you use the latter technique, you can change how you initialize SomeClass in another constructor. You can do that any way with the first code sample (unless you declare it final), but for some reason I'd feel less compelled to do so.

    Wondering what a pro might say here.

  3. #3
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'm no pro here either, but that won't stop me from putting in my 2 cents. For me I think that the two statements above do the same thing, but since your class can have multiple constructors, if you use the latter technique, you can change how you initialize SomeClass in another constructor. You can do that any way with the first code sample (unless you declare it final), but for some reason I'd feel less compelled to do so.

    Wondering what a pro might say here.
    On top of that, I think it might also be useful if you want to have the same initialization code for a variable if you define more than one constructor.

    Last but not least, i thought it might be used in situations where you omit the default constructor, but want to initialize these variables nonetheless. It would be more of a shorthand way to write the default constructor.

    Nevertheless, I am not sure if this is the reason why both ways are possible (and still not sure if they do the same thing under the hood :o).

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There's also the issue of order of execution, if that's important.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In my view if you run the complete code segments, then both of them do the same thing. But in the second case if and only if the Test class default constructor is called, object initialized. So as Fubarable says, multiple constructors change the initialization order in the second case.

Similar Threads

  1. Initialization Error
    By Always.Smile in forum New To Java
    Replies: 7
    Last Post: 10-12-2008, 02:04 PM
  2. 2D Array Initialization
    By M77 in forum Advanced Java
    Replies: 3
    Last Post: 06-04-2008, 02:12 PM
  3. initialization value problem
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 10:54 AM
  4. Lazy Initialization
    By onegcr in forum New To Java
    Replies: 1
    Last Post: 08-14-2007, 03:29 PM
  5. log4j initialization
    By arfatkhan in forum Web Frameworks
    Replies: 3
    Last Post: 08-10-2007, 07:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •