wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt in variable initialisation" Watch "doubt in variable initialisation" New topic
Author

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 {
2.int a=v();
3.int x=1000;
4.int 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();
11.}
12.}

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?
Thanx.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Check this thread...

http://www.coderanch.com/t/246397/java-programmer-SCJP/certification/little-help-class


"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
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in variable initialisation