This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What's the difference? (or is there one?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's the difference? (or is there one?)

Will Kasper
Greenhorn

Joined: Nov 25, 2012
Posts: 3
Hello all,

I was wondering if there is a difference between these two situations, and if so, which one should I use.

Situation 1:


Situation 2:


I assume that they both yield the same end result, but is one better to use than the other? Does situation 1 initialize the values at compile time while situation 2 initializes the values at run time? Or am I just splitting hairs here? Thank you so much, I look forward to your answers!
-Will
Suresh Sajja
Ranch Hand

Joined: May 12, 2009
Posts: 34

No difference. Both serve the same purpose.

Situation 1 works well when initialization can be put in one line.
Situation 2 works well when complex initialization process and error handling is required.


~Suresh
Suresh Sajja
Ranch Hand

Joined: May 12, 2009
Posts: 34

Will Kasper wrote: Does situation 1 initialize the values at compile time while situation 2 initializes the values at run time?


Initialization happens at run time.
In case of situation 1, the initialization code is actually put in to the constructor by the compiler
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
I think No 1 is less good style than No 2, but other people will disagree.
No 1 does not have the error in concept that No 2 has. Look at the comments: That is not a default constructor. This is what a default constructor looks like:-That is a no‑arguments constructor which does not have an empty body.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7501
    
  18

Seetharaman Venkatasamy wrote:I think No 1 is more readable as well as *bit* safe[r]...

I think you both have valid points; but I think that the overriding factor is when the value being assigned is available. If it is available at the time the class is loaded, I'd probably use 1; if not - and particularly if it relies on a value passed to the constructor - you really have no choice but to use option 2.

My usual rules of thumb when you have a choice:
1. Which is more readable?
2. If the field was final, which option would I use?

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Good question and Welcome to JavaRanch Will Kasper
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Winston Gutkowski wrote:. . . If it is available at the time the class is loaded, . . .
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.
Will Kasper
Greenhorn

Joined: Nov 25, 2012
Posts: 3
Seetharaman Venkatasamy wrote:Good question and Welcome to JavaRanch Will Kasper

Thank you, I'm glad to be here. I've lurked for a while but I decided it was time to sign up and become part of the community. I appreciate all the great answers.
Will Kasper
Greenhorn

Joined: Nov 25, 2012
Posts: 3
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.

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!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7501
    
  18

Will Kasper wrote:I like this. It sounds like a good reason to stick to situation 1.

Unless you can't.

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!

I don't really think there's any "right" way to do it, but I would say the even more important than "elegance" is readability. Personally, I find direct instantiation (option 1) easier to read, so I'll generally use it unless there's something preventing me from doing so.

Winston
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
I tend to favor 1 because it's more readable - everything you need to know about the field is in one place.

I would use 2 if I wanted an initial value that depended on some constructor parameter. Otherwise, probably not.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

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.


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. I think if variables are being initialized in a constructor, they should be always be initialized in constructors. If variables are initialized when they are declared, they shouldn't be re-initialized in any constructor. The class should be completely consistent in how it initalizes member variables. It makes the code easier to read.

Also, "Default" values lead to "Faults" :p I would rather get a NullPointerException (because a variable is not initialized ) during unit test than get wrong results (because the variable was initialized wrong) while testing it on the UI. If you have multiple constructors (and espescially if you are fond of calling this from your constructor), it's very easy to lose track of what is initialized to what in which case. If you add "default" values on top of that, you are adding another layer of complexity to your initialization code.

Personally, if you have multiple constructors, doing all the initialization inside the constructor leads to beautiful code. Generally, I put initialization in the declaration if I only need a no arg constructor. If I need more constructors, I move all the initialization code to constructors. It looks much nicer.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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 ? ;)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
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. For example:

If possible, make the fields final as well, to make it even more clear that they're initialized once and only once.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7501
    
  18

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...

Yup. I can see that point of view too. And I quite like your point about multiple constructors.

Winston
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2271
    
  28

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.


Yup Yup. Also, if you have a class that is not a bean, and you see that it has so many members and so many constructors that you are worrying about redundancy in your constructors, then you have bigger problems. The smell that I would be focusing on is:- too many dependencies, and not redundant code in constructors. I can understand a bean having lot of members. In that case, your approach works very well

Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Suresh Sajja wrote:
In case of situation 1, the initialization code is actually put in to the constructor by the compiler

How's that done?


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's the difference? (or is there one?)
 
Similar Threads
shadow variable doubt
Shadowing Object References?
A setter that can only be called by framework
Interpretation of Shadowing Object Reference variable Code from K&B
how to implement array of objects???