| Author |
Field access (local/static)
|
Pawanpreet Singh
Ranch Hand
Joined: Jun 12, 2005
Posts: 264
|
|
This topic is somewhat related to previous topic (Methods overridden/hidden/inherited) Now again one related question, as we know, we can't override fields (static/local), we can only hide them(hiding only need name of variable to be same), So below example related with variables access instead of methods call. /** OUTPUT IS overload.initStr :10 A.local() : A.Init : Dynamic Bind Detect HideStaticField.hi() */ As we know this reference is passed in the local() method call, any only non static methods will be called from class whose object, the reference is denoting. hide reference is denoting new HideStaticMemer() object, so hi() of HideStaticField is called. Further initStr is static memeber access which will accessed as field of class A. But My question is that in local() method, how is dynamic_bind_detect variable (non-static member)accessed from base class A not from HideStaticField class. ( tags added) [ June 16, 2005: Message edited by: Barry Gaunt ]
|
 |
rajan singh
Greenhorn
Joined: Jun 08, 2005
Posts: 17
|
|
public class Runner3 { public static void main(String[] args) { Base b = new Derived(); Derived d = new Derived(); System.out.println(b.id); System.out.println(d.id); } } class Base { String id = "i am base"; } class Derived extends Base { String id = "i am derived"; } output..... i am base i am derived above prg suggest that field access depends upon refernece type not upon actual object created. and this reference is implicitly passed...in base class this(reference) is of type of base(since base class has no idea about derived class)though it is pointing to object of subclass..that is the reason hidden field is printed... PLEASE LET ME KNOW IF I AM WRONG..... [ June 14, 2005: Message edited by: rajan singh ]
|
 |
Pawanpreet Singh
Ranch Hand
Joined: Jun 12, 2005
Posts: 264
|
|
|
Thanks Rajan, Great...
|
 |
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
|
|
so our final conclusion is even so even we have created HideStaticField hide = new HideStaticField(); but whenever we call to base class method and from that any variable is called ..it will be from base class itself and if its not present in base class ..compiler error.. I am rt?
|
Thanks and Regards,<br />Amit Taneja
|
 |
Pawanpreet Singh
Ranch Hand
Joined: Jun 12, 2005
Posts: 264
|
|
|
Yes Amit, In my view you are right
|
 |
janki tangeda
Ranch Hand
Joined: Jun 07, 2005
Posts: 54
|
|
--------------------------------------------------------------------------- As we know this reference is passed in the local() method call, any only non static methods will be called from class whose object, the reference is denoting --------------------------------------------------------------------------- Why is the this reference passed through the local method?Can anyone explain how we got that output.Its not yet clear to me.
|
 |
Pawanpreet Singh
Ranch Hand
Joined: Jun 12, 2005
Posts: 264
|
|
Actually the thing is that, we can only override the non static methods, we can not override static method or any variable (static/no static). So static methods can only be hidden in child class but should follow all rules for a method to be overridden. And static/non static variables of parent class can only be hidden in child class by just specifying same name(no matter what will be the datatype of that variable in child class). So both static methods and static/non static variables call will be done in same manner. Please check my message posted on June 14, 2005 03:27 AM, in same topic which was my original question. For more details see Methods overridden/hidden/inherited
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
Please use tags around your formatted code to enable us better to understand its structure.
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
 |
|
|
subject: Field access (local/static)
|
|
|