Results 1 to 5 of 5
Thread: Initialization
- 12-08-2008, 08:02 PM #1
Member
- Join Date
- Dec 2008
- Location
- Enschede, Overijssel, the Netherlands
- Posts
- 19
- Rep Power
- 0
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)
andJava Code:public class Test { private SomeClass sc = new SomeClass(); }
2)
My guess is that they do *exactly* the same thing, i.e. initializing the sc variable at the time new is invoked.Java Code:public class Test { private SomeClass sc; public Test() { sc = new SomeClass(); }
Is there any semantic difference between the two alternatives and if so, what exactly?
-
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.
- 12-08-2008, 10:56 PM #3
Member
- Join Date
- Dec 2008
- Location
- Enschede, Overijssel, the Netherlands
- Posts
- 19
- Rep Power
- 0
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).
-
There's also the issue of order of execution, if that's important.
- 12-09-2008, 05:29 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
Initialization Error
By Always.Smile in forum New To JavaReplies: 7Last Post: 10-12-2008, 02:04 PM -
2D Array Initialization
By M77 in forum Advanced JavaReplies: 3Last Post: 06-04-2008, 02:12 PM -
initialization value problem
By ravian in forum New To JavaReplies: 2Last Post: 01-28-2008, 10:54 AM -
Lazy Initialization
By onegcr in forum New To JavaReplies: 1Last Post: 08-14-2007, 03:29 PM -
log4j initialization
By arfatkhan in forum Web FrameworksReplies: 3Last Post: 08-10-2007, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks