~Suresh
Will Kasper wrote: Does situation 1 initialize the values at compile time while situation 2 initializes the values at run time?
~Suresh
Seetharaman Venkatasamy wrote:I think No 1 is more readable as well as *bit* safe[r]...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Wouldn’t that apply to static fields? I would usually initialise a static field in situ:It is often a bad idea to initialise a static field in the constructor, because it might change its value every time a new instance is created.Winston Gutkowski wrote:. . . If it is available at the time the class is loaded, . . .
Seetharaman Venkatasamy wrote:Good question and Welcome to JavaRanch Will Kasper
Seetharaman Venkatasamy wrote:
If you explicitly assigned value inside one constructor and forget to do in another then constructor dont do that for you which result some error.
Will Kasper wrote:I like this. It sounds like a good reason to stick to situation 1.
I was just wondering if one style was more *elegant* than the other. I try to follow best practices whenever possible, but sometimes it's not clear what those best practices are. I've seen both styles used in examples of code so I figured I'd try and get to the bottom of which one to use. Thank you all very much!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Seetharaman Venkatasamy wrote:I think No 1 is more readable as well as *bit* safe.
suppose there are two constructor MyExampleClass() and MyExampleClass(int flag). if you assign on declaration then compiler(rearrange) insert the values inside both constructor which is safe and still value will be assigned in case of new MyExampleClass() as well new MyExampleClass(int flag).
If you explicitly assigned value inside one constructor and forget to do in another then constructor dont do that for you which result some error.
just my 2 cents.
Jayesh A Lalwani wrote: If I need more constructors, I move all the initialization code to constructors. It looks much nicer.
Seetharaman Venkatasamy wrote:
Jayesh A Lalwani wrote: If I need more constructors, I move all the initialization code to constructors. It looks much nicer.
dont you smell redundancy ? ;)
Jayesh A Lalwani wrote:Sorry, this is the reason, I wouldn't do it. When you see a variable initialized along with the declaration, the mind tends to get fixated on that initialization, and it's very easy to miss that the variable has been initialized differrently in one particular constructor...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mike Simmons wrote:
Seetharaman Venkatasamy wrote:
Jayesh A Lalwani wrote: If I need more constructors, I move all the initialization code to constructors. It looks much nicer.
dont you smell redundancy ? ;)
Very little if you do it right. Have one constructor that initializes everything. Any other constructors forward to the main constructor, passing in any other args they need to.
Suresh Sajja wrote:
In case of situation 1, the initialization code is actually put in to the constructor by the compiler
The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in