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