wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Forward referenced variable in initializer block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Forward referenced variable in initializer block" Watch "Forward referenced variable in initializer block" New topic
Author

Forward referenced variable in initializer block

Vladimir Popel
Greenhorn

Joined: Jan 20, 2007
Posts: 10
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

Joined: Feb 23, 2007
Posts: 447
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

Joined: Feb 28, 2007
Posts: 1707
See this link




Regards,
cmbhatt
[ April 20, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Vladimir Popel
Greenhorn

Joined: Jan 20, 2007
Posts: 10
Thanks for help guys
sandeep atluri
Ranch Hand

Joined: Apr 19, 2007
Posts: 42
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
sandeep atluri
Ranch Hand

Joined: Apr 19, 2007
Posts: 42
nice post chandra... thank you very much....

keep posting material like that....
sharan vasandani
Ranch Hand

Joined: Feb 22, 2007
Posts: 100
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

Joined: Feb 23, 2007
Posts: 447
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

Joined: Jul 13, 2006
Posts: 73
Hi All.


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

Thanks.
 
 
subject: Forward referenced variable in initializer block
 
Similar Threads
Forward Reference
plzz explain the output
initializers-referencing
resultset problem
Block execution