This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
While this is somewhat of a guess, I believe the correct answer is that your call to giveMeJ() happens PRIOR to the assignment statement in the line below it. Remember that when an int is first created, it is assigned the value of 0 until you explicitly assign it a different value.
Tu Ran, make sure you understand how initialization take place. Not to complicate matters, but there are 7 type of variables! The type of variables are: (from JLS: 4.5.3 Kinds of Variables) 1) static fields variables 2) instance fields variable 3) local 'automatic' variable 4) method parameter variables 5) constructor parameter variable 6) exception-handler parameter variable 7) array component variable your example code has only the first kind 'static fields variables' What you need to understand about initialization is this: 1. variable are initialized in their order of appearance 2. static initialization takes place first, and only happens 'one time', when the class is loaded; the variables are initialized by their default values (if no value is assigned to them) 3. instance variables (type 2) are initialized each time an object is created; the variables are initialized by their default values if no value is assignment to them 4. type 3 variable need to be explicitly assignment a value before their use 5. types 4, type 5 and type 6 are assigned values by the JVM during the execution of a process 6. array variables (type 6) are always initialized to some default value(s) if not explicitly initialized You want to know why you see a value of '0' rather than a value of '10'? When class Aquestion is loaded the 'two static fields i and j are initialized with a default value of a integer '0', next a call is made to giveMe() which return the value stored in j. Since j is declared after i, j will not have been given a change to get initialized with a value of '10', so j's default initialized value of '0' is what get return and assigned to i Quite straight forward isn't it
<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Just an an after-thought! Try switching... static private int i = giveMeJ(); static private int j = 10; To... static private int j = 10; static private int i = giveMeJ(); This explains the sequence of initializations!
SCJP 1.4, SCWCD 1.3
Joined: Jan 18, 2002
Actually Vijay, if we swap the statements then we don't highlight the fact that static fields first get initialized to their default values (zero in this case), the original statements clearly demonstrate this! Your suggestion would fails to show that j is initialized with a default value of 'zero' when the class is first loaded and then assigned the value of '10' when the statement 'static private int j = 10;' is reached. Hope this clears up the process for you.
Originally posted by Vijay Albuquerque: Just an an after-thought! Try switching... static private int i = giveMeJ(); static private int j = 10; To... static private int j = 10; static private int i = giveMeJ(); This explains the sequence of initializations!
[ March 02, 2002: Message edited by: Rajinder Yadav ]