• 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

Please help me! About the instance initialization block!

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pleaon explain for me why the assign a certain value to a variable in instance initialization block is ok but increment ( or st like that) is not

I thought that the instance initialization block is called after all super classes of this class are called. At that time all instance variables have been initialized. So why I can't increment or add a value to it?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every thing is fine except the way you have declared the variable , the compiler will complain about illigal forward refrencing, just declare the variable before the initialization block , there will be no compilation problem
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you cannot access the value of a field before its declaration (as Mahima mentioned). That's why you are getting an error. Try these too

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:you cannot access the value of a field before its declaration (as Mahima mentioned). That's why you are getting an error. Try these too



but why the bold part is not cause compile error.
we read value of b and assigned to a ..?
confused...
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assigning a variable:
So for assigning a value to a variable in an initialization block I don't need to declare the variable before. I can do it afterwards.
But if I do it outside of the initialization block I MUST declare the variable before the assignment:

Accessing a variable:
But if I access a value, e.g. by using it as a parameter, I have to declare the variable before accessing it, independently if it is used within
or outside an initialization block:

These statements are true for static or instance initialization block ???
Any errors in my statements?

Thanks
Bob
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Welly Tambunan wrote:

Ankit Garg wrote:you cannot access the value of a field before its declaration (as Mahima mentioned). That's why you are getting an error. Try these too



but why the bold part is not cause compile error.
we read value of b and assigned to a ..?
confused...


The problem makes me be confused is the time at which instance initialization block ewxecuted. Theoretically,it's executed right after the call to supper() in a constructor. But at that time,all instance variables have been initialised.so why they can't be accessed.
And another question is: if they can be accessed,why they can be assigned valued?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Welly Tambunan wrote:

Ankit Garg wrote:you cannot access the value of a field before its declaration (as Mahima mentioned). That's why you are getting an error. Try these too



but why the bold part is not cause compile error.
we read value of b and assigned to a ..?
confused...



When you say



then you are not assigning the value of b to a. You are assigning 20 to a. It is equivalent to



There are some ways to confirm it but I don't want to confuse you...
 
Welly Tambunan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:


There are some ways to confirm it but I don't want to confuse you...



oo.. i see. So i was wrong about that code. Can you give me another way to confirm it?
I'm interesting to take a look at it.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well if you want to know, then take a look here...
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excuse me! The problem makes me be confused is the time at which instance initialization block ewxecuted. Theoretically,it's executed right after the call to supper() in a constructor. But at that time,all instance variables have been initialised.so why they can't be accessed.
And another question is: if they can be accessed,why they can be assigned valued?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yen hoang wrote:Pleaon explain for me why the assign a certain value to a variable in instance initialization block is ok but increment ( or st like that) is not



JLS (§8.3.2.3) has this to say :

The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:

1. The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.

2. The usage is not on the left hand side of an assignment.

3. C is the innermost class or interface enclosing the usage.

By incrementing (y++) which is equivalent to (y = y+1) , you implicitly used y at the right side of an assignment. Just IMHO though :-)

Interestingly, if you change y to static int in your declaration, the compiler will not complain anymore See JLS (§8.3.2.2 )
 
yen hoang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS (§8.3.2.3) has this to say :

The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:

1. The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.

2. The usage is not on the left hand side of an assignment.

3. C is the innermost class or interface enclosing the usage.

By incrementing (y++) which is equivalent to (y = y+1) , you implicitly used y at the right side of an assignment. Just IMHO though :-)

Interestingly, if you change y to static int in your declaration, the compiler will not complain anymore See JLS (§8.3.2.2 )

thank you very much. I understood!
 
reply
    Bookmark Topic Watch Topic
  • New Topic