aspose file tools*
The moose likes Beginning Java and the fly likes explicit vs instance initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "explicit vs instance initialization" Watch "explicit vs instance initialization" New topic
Author

explicit vs instance initialization

M Berg
Greenhorn

Joined: Sep 01, 2010
Posts: 13
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
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

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.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

After the edited query, I would go with Campbell's method
M Berg
Greenhorn

Joined: Sep 01, 2010
Posts: 13
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3099
    
  33

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.
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
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
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
There is also the option of using factory methods, but that is rather beyond the "beginning" stage.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: explicit vs instance initialization