hi guys
cool it
take the original code and 4get all the reeplies to it for the time being
the JRE parses the .class file many times
in the first few parses creation of the base class Object etc take place by default
now when it comes to creating the object of this class
all the declarations undergo the 1st pass
in this he finds the declarations
int i
int j
public giveMe()
there r 3 memory locations initialized now
each of 32 bits
1st for variable i
2nd for variable j
3rd for giveMe()
remember JRE does not look for the assigment / defining the variables in this parse .... it only declares them n initializes the chunks of memory to them.
But at the same time JRE assigns a default value to the variable
which is 0 in case of int
so now
1st int i=0
2nd int j=0
now in second parse JRE defines/ assigns the variables the given values
so now at this parse JRE 1st finds the assignment to i which is
( unfortunately
) a function named giveMe()
now giveMe prints called and then returns j
and right now j is assigned 0 by default coz JRE has not yet reached the j's definition which j = 10;
so this function returns 0 and assigns it to i
then JRE proceeds to the next line n finds definition of j and assigns the value 10 to j
this is how the o/p of the code is 0
now the next query about the final keyword
here at the first parse itself JRE finds the declaration
final int j = 10;
now final tells JRE that this variable not only has to be initialized with memory but also has to define/assign the value 10 to it coz this value can never be changed later
therefore when u put final j is given its 32 bits n also assigned to 10 immediately on the 1st parse itself
now on the second parse JRE finds giveMe()
n it returns 10 this time which is assigned to i
so the code with final prints 10
bye
Deepak