• 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

difference between Instance Initialization and Initialization at the point of Definition

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

In Thinking in Java 4E, They described 4 ways to keep references initialized.

1. Initializing at the point of definition
2. In the constructor
3. at the point of use (lazy initialization)
4. Instance Initialization

and they given an example for it.



Can any one explain me the difference between Instance Initialization and Initialization at the point of definition except the location at which they will be done.

i mean is there any difference in the scope of those variables ??

Regards
Balakrishna T
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no difference in scope.

As far as I can see,

The Only difference is the order of Initialization. Initialization at the point of definition is executed before the Instance initialization.
 
Balakrishna Thati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Samrat,

I guessed that one and tried different relative positions for Instance initialization and Initialization at the point of definition statements.

you can see that in the example i provided. There i commented the later part.

When you run my program, Instance initialization will be done first and Next Initialization at the point of definition.
But if you comment out the first block of Instance Initialization and uncomment the later on, Initialization at the point of definition executes first and then Instance initialization.

They are executing in the order they are provided. Which is pretty normal behaviour.

so i thought there should be some other difference between them.

Regards
Balakrishna T
 
Samrat Som
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bala,

You are correct. I think The order is the only reason that can be summarized. Even I tried it out and the result is as expected.
We can conlcude that Intance Initialization is the same as the Initialization at the point of definition. The compiler must be converting the initialization at the point of definition in the Instance Initialization in the order it has been mentioned.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In terms of order, there is really no difference between the initialization of an instance variable (with the declaration), or the execution of an instance initializer. The specification states that these are done, in order they are encountered (in source). So either the instance variable initialization, or instance initializer can come first, depending on how the class is coded.

The reason an instance initializer is sometime "preferred", is when you have a complex initialization -- maybe temporary interium variables are needed. Maybe branches or loops are needed. And it is better to just have a block of code than an expression.

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

if you don't mind, i didn't clearly understood your last statement.

can you give an example illustrating it:

"The reason an instance initializer is sometime "preferred", is when you have a complex initialization -- maybe temporary interium variables are needed. Maybe branches or loops are needed. And it is better to just have a block of code than an expression"


Regards
Balakrishna Thati

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please use code tags when you post source code.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balakrishna Thati wrote:Hi Henry,

if you don't mind, i didn't clearly understood your last statement.

can you give an example illustrating it:

"The reason an instance initializer is sometime "preferred", is when you have a complex initialization -- maybe temporary interium variables are needed. Maybe branches or loops are needed. And it is better to just have a block of code than an expression"



Let's say the initialization of i was a bit more complex (in the original post) -- something like this...



Okay, now, let's say you don't want to use the instance initializer, how will you do this with an expression, during the initialization at the time of declaration? Obviously, you can't..... Another option is to place it into the constructor, but what if you had dozens of constructors?

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic