This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes doubt in variable initialisation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt in variable initialisation" Watch "doubt in variable initialisation" New topic

doubt in variable initialisation

rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Hi guys
Could anyone explain me the behaviour of this code?

1.public class A { a=v(); x=1000; b=v();
5.public void v() {
6.System.out.println("x is"+x);
7.return x;
8. }
9.public static void main(String as[]) {
10.A aobj = new A();

when i compile and run this code it first prints x is 0
then it prints x is 1000;
I dont understand how it says x is 0? once the v() method is invoked, directly it goes to that method and i think during this the compiler doesnt have any idea about x.Then how it assumes x to be an integer and gives its default value?
But if i change the line 2 as
int a=x;
it says illegal forward reference.
could anyone explain me why?
marc weber

Joined: Aug 31, 2004
Posts: 11343

Check this thread...

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
subject: doubt in variable initialisation
It's not a secret anymore!