when we used the static for 2nd program the value in "value" is 30 unlike the 1st... is it that for static members the derived class maintains a separate copy of the variable from that of the parent class(where the value of "value " is altered)
in the first case call to addValue is polymorphic inside the constructor of Base. This is why the answer is 40. In the second example, the call to addValue is not polymorphic as addValue is static. So it is not overridden in the Derived class...
We cannot override the Static methods, static methods will be called based on the object reference type not on the object type. So in the second case the statment b.getValue() will execute the object reference(Base class) type's method not the object(Derived class) type's method. i.e. It will execute the Super class getValue()
to understand this we need to know that run time polymorphism means that, the function which we call at runtime is decided by its type of object. Like if class A and class B(class B is subclass of class A) both have method named getValue() then even you use the reference of super class the method will be called of the class of which object is being used.
The same thing is happening in the code snippet you gave before. When the JVM sees this it gets to the constructor of derived class and then to its super class without completing the derived class constructor. Now when the addValue function is called from the super class constructor then addValue of derived class is called as the object belongs to this class. Same happens when the control comes back to the derived class function call. and hence the function names 'addValue()' is called twice. So the output is 40.
For the later snippet of code which is giving output 30 you need to know that static functions are not overridden. Try again to understand it. I am sure that you will get its logic too.
thanks Harvinder, i had gone through the link.. source of the program wasSource of the program But now i have got another doubt...
in scjp5 by K & B page 130 its given that we cannot invoke the method instances or access any static variables untill after the super constructor runs. so my doubt is whether the super constructor has to complete or it has to be just run and there after we can access the instance methodsif we look at the 1st program(one whose output is 40) the overloaded version of the method is called in the base class constructor before the constructor actually completes...so is it that its just enough if super constructor runs without being completed as in above case