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

L

lirrar

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
 
V

Venkatesh

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.
 
C

Chris Smith

Venkatesh said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

James McGill

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.
 
E

Eric Sosman

James McGill wrote On 04/19/06 14:12,:
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.

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.
 
A

Andrew McDonagh

Eric said:
James McGill wrote On 04/19/06 14:12,:

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.

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.
 
V

Venkatesh

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
 
Joined
Apr 16, 2010
Messages
1
Reaction score
0
A possible solution will be to call static method via virtual method.
something like:

public class T1 {
public void f(){}
static public void stat(){
System.out.println("T1:stat...");​
}
public static void main(String []s){
T1 t = new T2();
t.f(); //will call T2.stat ..​
}​
}

class T2 extends T1 {

public void f(){
stat();
}

static public void stat(){
System.out.println("T2:stat...");
}​
}
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top