• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Sajja
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question and Welcome to JavaRanch Will Kasper
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 4796
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic