• 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

explicit vs instance initialization

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi experts,

is there some rule of thumb choosing between instance variables explicit initilization and instance initilization ?



EDIT:
I mean how to choose coding style a or b...

a)

b)


Many thanks,
Kind regards MB
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Berg wrote:Hi experts,

is there some rule of thumb how to choose between instance variables explicit initilization and instance initilization?


Many thanks,
Kind regards MB



The instance initialization blocks are executed in the order of their occurrence.

In the above code example: First the declaration+initialization followed by the initialization block.

You can try changing the value assigned to quality in the block after the declaration.


 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say as a rule of thumb for beginners, initialise all your fields in the constructor. Make sure not to forget any, otherwise you may suffer a NullPointerException (or calculate an incorrect value for a primitive).
You may have good reasons for initialising fields outside the constructor, but look what is happening. Your Tomato class has the problem that its "quality" is always A. Do you never have any B tomatoes, or even C tomatoes, which are only useful to throw at people when you have had too much to drink? This is what I suggest:That has the problem that people can pass "Excellent" or "Hippopotamus" as qualities rather than A B C; you could avoid that drawback by having "quality" a member of an enum.
If you are going to initialise your fields with default values, you can do it like this . . . but I personally would still do it in the constructor
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After the edited query, I would go with Campbell's method
 
M Berg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, but if we keep initializing outside constructor.
I am confused about the difference between instance variables explicit initialize (a, commented) and instance initialize block (b)?



Many thanks,
Kind regards MB




 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M Berg wrote:
I am confused about the difference between instance variables explicit initialize (a, commented) and instance initialize block (b)?



I dont think there's any difference. In the former- declaration and initialization are done together but in the latter- declaration and initialization are done in different statements.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok the advantages of using a constructor vs. using explicit initialization vs. instance init block could be like such:

An instance constructor can take arguments and can be overloaded. This allows you to set the object state according to external input. That is something obviously you can't do when you use explicit initialization or an init block.

An explicit initialization is limited to simple assignments such as like what you did in your example: private String quality = "A"; You can't get too much more complex than that. You're limited to basically 1 line of code that results in the assignment.

An instance initialization block will let you execute multiple lines of code...try/catch exceptions, etc...and you can do complex calculations if necessary to arrive at your final value.

Additionally, keep in mind that the compiler copies instance init block code into all constructors...so this is a good way of sharing common initialization code between multiple constructors if you should need to do so. Here's some more explanation:

http://download.oracle.com/javase/tutorial/java/javaOO/initial.html

I don't think there's any real rule of thumb so to speak...just use whatever fits best for the given scenario once you make sure that you fully understand the pros and cons of each approach. Once you get the grasp of the concepts then it will become pretty evident to you which usage is best for given scenarios. And thanks for posting this question, by the way. It forced me to recall some basics about which I hadn't thought much lately and reminded me of potentially powerful techniques that I don't often use but are available nonetheless.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also the option of using factory methods, but that is rather beyond the "beginning" stage.
 
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic