This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forward referenced variable in initializer block

 
Vladimir Popel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code:

=======================

class TestClass {

int i = getInt();

{
System.out.println(i); // no error, var i known
j = 20; // no error
System.out.println(j); // error here, forward ref to j
}

int j;

int getInt() {
System.out.println(j); // no error here!!!
return 10;
}
}

===========================

Can someone please explain why in an instance initializer you can assign a forward referenced variable but you can't use it (print it for example)?

I seems that in a method you can both assign and use a forward referenced variable...

What's the rule for this behavior?

Thanks in advance
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

Cristian Patitoiu

=============================================================
class TestClass {

int i = getInt();

{
System.out.println(i); // no error, var i known
j = 20; //line 1
System.out.println(j); // line 2
}

int j;

int getInt() {
System.out.println(j); // no error here!!!
return 10;
}
}
====================================================
The rule is
the variable can be used right side of the assignment operator .And we cannot read the value even the default value utill object of the super class is created (in your case Object class ).

in your code at line 1 you are using right side so no problem
line 2
you are reading the value there is a problem
[ April 20, 2007: Message edited by: anil kumar ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this link




Regards,
cmbhatt
[ April 20, 2007: Message edited by: Chandra Bhatt ]
 
Vladimir Popel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for help guys
 
sandeep atluri
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lets evaluate what you have done..once again....

in the first code you have written as follows:

int j=20;
System.out.println(j);

here. what you have done is that you have assigned a value of 20 to the variable j and later you are asking it to be printed...

lets.. see the second code.

int j;
int getInt()
{
System.out.println(j);
return 10;
}...


here you had declared a variable j but not initialized it...
but you are asking j to be printed , before you have even initialized it...
thats where the problem comes in.. its called
"illegal forward reference"..
which means that you cannot print a variable's value without initializing it......ok.... is that clear now....
 
sandeep atluri
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice post chandra... thank you very much....

keep posting material like that....
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my question is instance variables are assigned default value and then the superconstructors are called,

and then the intance initializer blocks are run as if they were part of constructor,if that is the case that means we are printing j's value after j has got its defualt value,then 20 and then we are printing it.

then why its giving a error.

regards,
sharan
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
sharan

Instance variables ,initializer blocks are executed in the order in which they are declared.
[ April 20, 2007: Message edited by: anil kumar ]
 
Pankaj Patel
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.


If instance variable and initializer block are executed in order they appear then, why error is not given here???

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic