aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt Reg: initializing Variables with method call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt Reg: initializing Variables with method call" Watch "Doubt Reg: initializing Variables with method call" New topic
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: 2367
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: 2367
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt Reg: initializing Variables with method call