This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 41124
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).

Ping & DNS - my free Android networking tools app
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thank you very much
I agree. Here's the link:
subject: output of this program?
Similar Threads
default constructor
Variable initialization
forward refrerences
Forward Referencing
please explain why the compiler is not complaining and why the output is 0