This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hiding Members (static methods /fields) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Hiding Members (static methods /fields)" Watch "Hiding Members (static methods /fields)" New topic
Author

Hiding Members (static methods /fields)

Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

class A
{
static String onlyme="A.Only Me";
static String initStr = "A.Init";
String dynamic_bind_detect = "Dynamic Bind Detect";
public void local() // never called in this program
{
System.out.println("A.local() :"+initStr+":"+dynamic_bind_detect);
hi();

}
public void hi()
{
System.out.println("A.hi()");
}
}
public class OverloadStatic extends A
{
protected static int initStr = 10;
public int dynamic_bind_detect = 10000;

public void hi()
{
System.out.println("OverloadStaticField1.hi()");
}

public static void main(String args[])
{
OverloadStatic overload = new OverloadStatic();
System.out.println("overload.initStr :"+overload.initStr);
overload.local();
}
}

output is

/**

A.local() :A.Init ynamic Bind Detect

OverloadStaticField1.hi()

*/

As i know, hi() of OverrideStatic is called. 'this' reference is passed to local() of A, so finally dynamic method binding, class of object is checked and hi() of OverloadStatic is called.

Similarly is case of dynamic_bind_detect of OverloadStatic, but it is of A.

Could you please advise that why dynamic_bind_detect of OverrideStatic class is not displayed as the actual object is passing this reference implicitly into local() method call.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Check this thread:

Only overridden instance methods are bind to run time polymorphism.
Others instance variables, static variables, static overridden (hidden) methods, and overloaded methods are all binded at compile time depending on the type of the reference variable and not on the object.
Pawanpreet Singh
Ranch Hand

Joined: Jun 12, 2005
Posts: 264

Thanks a lot
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hiding Members (static methods /fields)
 
Similar Threads
Question Regarding Boxing
instance varibles and static methods????
Methods overridden/hidden/inherited
Abstract classes
Field access (local/static)