It's not a secret anymore!
The moose likes Beginning Java and the fly likes output of this program? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "output of this program?" Watch "output of this program?" New topic
Author

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
Marshal

Joined: Mar 22, 2005
Posts: 35256
    
    7
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).


Android appsImageJ pluginsJava web charts
Anto Telvin
Ranch Hand

Joined: Aug 12, 2008
Posts: 113
thank you very much
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: output of this program?
 
Similar Threads
default constructor
forward refrerences
please explain why the compiler is not complaining and why the output is 0
Forward Referencing
Variable initialization