Hello all, Please format your sample code by using < pre > code sample < /pre > . While typing pre tags don't include the space in bet < and > . Thanks for your coperation.
<pre> class Shape { static int x = 7, y = 7; int a_big_no; int Sizeof; int getY() {return y;}; } class SubShape extends Shape { int x =5, y =5; int getY() {return y;} } class upcast extends Object { public static void main(String args[]) { Shape a = new Shape();//creates instance of shape Shape b = new SubShape();//Creates instance of subshape System.out.println("a.y = " + a.y + " b.y = "+ b.y);//prints 7 and 7 System.out.println("a.getY() = " + a.getY() + " b.getY() = " + b.getY());//prints 7 and 5 } } </pre> Why Such behavior ??? why cannot there be dynamic binding for variables??? Why only for methods??? Thanks............
[This message has been edited by maha anna (edited April 19, 2000).]
In my openion, since the above variables are static their values are known at compile time itself. Whereas, the exact method which gets invoked is not known till runtime. Hence you see the difference. I hope others can explain in more detail....till then a search could reveal previous discussions on similar topics which could help you. Regds. - satya
Even if they weren't static, it would have behaved the same way. Variables are always bound at compile time, unlike method invocation which is deferred to runtime resulting in Polymorphism.
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.