Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Can static methods be virtual i.e. invoked polymorphically?

Thread view: 
lirrar@yahoo.com - 19 Apr 2006 10:52 GMT
Can static methods be virtual i.e. invoked polymorphically?
The options as below.

a  yes
b  no
c  it depends on the class declaration
d  Wrong Question
Venkatesh - 19 Apr 2006 13:16 GMT
I think static methods don't show polymorphic behavior because static
methods are bound to classes.

Having the same static method in both base and derived class doesn't
show any compilation error. I tried the following and there was no
compilation error:

class BaseC1 {

   public static void f1() {
       System.out.println("BaseC1.f1");
   }

}

class DerivedC1 extends BaseC1 {

   public static void f1() {
       System.out.println("DerivedC1.f1");
   }

}

But, when f1 is called from a reference to an object of base, then f1
in base class is called and if f1 is called from reference to an object
of derived, then f1 in derived class is invoked.
Chris Smith - 19 Apr 2006 16:58 GMT
> I think static methods don't show polymorphic behavior because static
> methods are bound to classes.

Yep.

> But, when f1 is called from a reference to an object of base, then f1
> in base class is called and if f1 is called from reference to an object
> of derived, then f1 in derived class is invoked.

First of all, it's moderately broken (but technically legal) to call f1
using a reference in the first place.  The compiler in Eclipse can be
set up to give you an error or warning when you do it, and CheckStyle
can do the same.  These are good things.  The correct way to call f1 is
as follows:

   BaseC1.f1();       OR
   DerivedC1.f1();

In either case, it's clear which of the two methods is being called.  
Remember, just say not to calling static methods using a reference.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Venkatesh - 20 Apr 2006 05:26 GMT
Hi Chris,

Thanks for your note. You are right. Even I always ensure that static
functions are called using the class name and not any reference. I was
just trying to see if calling from references by any chance makes the
functions show polymorphic behavior. But, this behavior was not shown
....

Thank you,
Venkatesh
James McGill - 19 Apr 2006 19:12 GMT
> But, when f1 is called from a reference to an object of base, then f1
> in base class is called and if f1 is called from reference to an
> object
> of derived, then f1 in derived class is invoked.

Doesn't that depend on what the compile-time type of each respective
object was?  

It's a misconception that a static method is *ever* "called from a
reference to an object."  That simply never happens.  If you
syntactically call a static method on an object reference, it is
compiled as a static method call on whatever class that object happened
to be at compile time.
Eric Sosman - 19 Apr 2006 20:02 GMT
James McGill wrote On 04/19/06 14:12,:

>>But, when f1 is called from a reference to an object of base, then f1
>>in base class is called and if f1 is called from reference to an
[quoted text clipped - 9 lines]
> compiled as a static method call on whatever class that object happened
> to be at compile time.

   One of the clearest demonstrations of this point
is to set the reference to null before using it to
call the static method.  Since the method actually
does get called even though there's no object lying
around, it's obvious that the run-time type of the
object has nothing to do with the matter.  All that
counts is the compile-time type of the reference.

Signature

Eric.Sosman@sun.com

Andrew McDonagh - 19 Apr 2006 23:08 GMT
> James McGill wrote On 04/19/06 14:12,:
>>
[quoted text clipped - 19 lines]
> object has nothing to do with the matter.  All that
> counts is the compile-time type of the reference.

Yep - and thankfully now most IDEs will warn (at least) that you are
caling a static method on an object instance, rather than a class.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.