Storage
-
Local variable (including method parameters): stored with record
for each method call. (In reality, determined by compiler. The
compiler freely introduces and removes local variables. They may
be stored in register or not stored at all.)
-
Object variable (field): stored with object.
-
Class variable (static field): stored with class.
Sharing
-
Local variable: separate for each method call, even on the same object.
-
Object variable (field): shared by method calls on the same object.
-
Class variable (static field): shared by all objects.
Initialization
-
Local variable: initialized when method called.
-
Object variable (field): initialized when object is first created.
-
Class variable (static field): initialized when class is loaded
(before any object created).
See XFields.java
Here is the state just before the main function ends: