File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes output of this program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "output of this program?" Watch "output of this program?" New topic

output of this program?

Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
hi ranchers
i have the following code :

public class AQuestion
private int i = giveMeJ();
private int j = 10;

private int giveMeJ()
return j;
public static void main(String args[]){
System.out.println((new AQuestion()).i);
why i am getting 0 as output.what is happened here?

thanks in advance

Anto Telvin Mathew<br />Many of the life failures are people who did not realize how close they were to success when they give up. EDISON
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42952
Because the initializations of the fields are done in the order in which they appear in the source code. So "i" is initialized before "j", and at the time of the initialization, "j" still has its default value (which is 0).
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thank you very much
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: output of this program?