File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Field access (local/static)

 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 264
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rajan, Great...
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Amit, In my view you are right
 
janki tangeda
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------------------------------------------------------------------
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
Posts: 264
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use tags around your formatted code to enable us better to understand its structure.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic