| Author |
Doubt Reg: initializing Variables with method call
|
Ravi Pinnaboyina
Greenhorn
Joined: Feb 19, 2007
Posts: 11
|
|
How the output is printed as Zero....... How is the value of i being resolved ? private int i=getData(); private int j=10; int getData() { return j; } public static void main(String arg[]) { System.out.println(""+new ASample().i); }
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
The value of j is read in the method before it has been initialized.
|
 |
Ravi Pinnaboyina
Greenhorn
Joined: Feb 19, 2007
Posts: 11
|
|
So it means that i and j will only be declared first only then assignment starts... i.ie., int a=10; int b=10; first a nd b will be declared only then they get initialized??? Am I right??
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
The instance variables are in scope, but initialization occurs from top to bottom in the class definition. Since the call to the method accesses j before it is initialized, it has its default value.
|
 |
Anton Uwe
Ranch Hand
Joined: Jan 10, 2007
Posts: 122
|
|
is the same as
|
 |
 |
|
|
subject: Doubt Reg: initializing Variables with method call
|
|
|