Will a static variable from the parent class be inherited as is to the child class, or will a new variable be created?
For example, should a static counter variable of class A
and class B extends A
give the same value (if we have not defined a new counter for class B
) ?
No, Static variable does not behave like non-static variables. If you change value of static variable with one of the inherited class it will effect all other inherited class data.
Cause static variable is created only once. Even though you are creating multiple objects the static variables are not created again and again. They are created at starting time of execution and stored. When ever you access static variable you will get same variable. i.e
if you are accessing as B.count or C.count you will get same variable. So you are having only one variable then you can't maintain count separately for two objects in single variable.