Accessing static method of super-class (generics)

  • Thread starter Vidar S. Ramdal
  • Start date
V

Vidar S. Ramdal

I'm twisting my brain on this problem, which is probably really
obvious, but I'm stuck.

I have an abstract class X with a static method:

public abstract class X {
public static void method() {
...
}
}

Class X is extended by many classes, e.g. class Y:

public class Y extends X {
...
}

Now, in a separate (test) class, I have a method that takes for example
class X as parameter, and should call method() on it:

public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
clazz.method(); // Compile error
}
}

Why can't I call clazz.method()?
I'm probably able to get around it using reflection, but there surely
must be a simpler way?
 
S

shweta

Vidar said:
I'm twisting my brain on this problem, which is probably really
obvious, but I'm stuck.

I have an abstract class X with a static method:

public abstract class X {
public static void method() {
...
}
}

Class X is extended by many classes, e.g. class Y:

public class Y extends X {
...
}

Now, in a separate (test) class, I have a method that takes for example
class X as parameter, and should call method() on it:

public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
clazz.method(); // Compile error
}
}

Why can't I call clazz.method()?
I'm probably able to get around it using reflection, but there surely
must be a simpler way?
 
C

Chris Uppal

Vidar said:
public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
clazz.method(); // Compile error
}
}

Why can't I call clazz.method()?

It's nothing to do with generics, but just that you can't use an instance of
java.lang.Class to invoke static methods of the corresponding class. Or
rather, you can, but only by using reflection.

(It may help to think of instances of java.lang.Class not as "being" the
classes in question, but as placeholders that each /identify/ a class[*].)

I'm probably able to get around it using reflection, but there surely
must be a simpler way?

Nope; reflection (or a change to your design) is the way to go.

-- chris

([*] "In Java", as we Smalltalkers like to point out, "classes are not
objects")
 
S

shweta

Vidar said:
I'm twisting my brain on this problem, which is probably really
obvious, but I'm stuck.

I have an abstract class X with a static method:

public abstract class X {
public static void method() {
...
}
}

Class X is extended by many classes, e.g. class Y:

public class Y extends X {
...
}

Now, in a separate (test) class, I have a method that takes for example
class X as parameter, and should call method() on it:

public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
clazz.method(); // Compile error
}
}

Why can't I call clazz.method()?
I'm probably able to get around it using reflection, but there surely
must be a simpler way?

Hi vidar

Very basic :static method is a property of a class.
It should be called : className.methodName();

If this method is getting overridden by a childClass
then it should again be made static

If you want to call static method of parent class it should be
parentClassName.methodName

for child class
childClassName.methodName

Once again static method is a class property It should be called by
using class name which contains this method
 
T

Thomas Hawtin

Vidar said:
I have an abstract class X with a static method:
Class X is extended by many classes, e.g. class Y:
Now, in a separate (test) class, I have a method that takes for example
class X as parameter, and should call method() on it:

public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
clazz.method(); // Compile error
}
}


If I were you, I wouldn't be starting from there.

Instead of passing a Class, pass an object of a type that does have the
method declared. Or define a mapping from Class to such a type.

Tom Hawtin
 
V

Vidar S. Ramdal

Chris said:
[...]
(It may help to think of instances of java.lang.Class not as "being" the
classes in question, but as placeholders that each /identify/ a class[*].)

Right, that makes sense.
Nope; reflection (or a change to your design) is the way to go.

OK, reflection it is, then. Thanks for the explanation!
 
O

Oliver Wong

Vidar S. Ramdal said:
I'm twisting my brain on this problem, which is probably really
obvious, but I'm stuck.

I have an abstract class X with a static method:

public abstract class X {
public static void method() {
...
}
}

Class X is extended by many classes, e.g. class Y:

public class Y extends X {
...
}

Now, in a separate (test) class, I have a method that takes for example
class X as parameter, and should call method() on it:

public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
clazz.method(); // Compile error
}
}

Why can't I call clazz.method()?
I'm probably able to get around it using reflection, but there surely
must be a simpler way?

You've been told why you can't call clazz.method(), but I'm a bit
concerned here about your design, as it implies you may have a
misunderstanding of how method overriding works. You don't need to use
generics or reflection or anything like that to call the static method. You
could just call it like this:

public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
x.method();
}
}

If you've defined another static method in class Y called "method",
thinking that it will somehow override the static method defined in class X,
you're mistaken. See
http://java.sun.com/developer/onlineTraining/new2java/supplements/2003/Jun03.html#4

- Oliver
 
V

Vidar S. Ramdal

Vidar S. Ramdal said:
[...]
public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
clazz.method(); // Compile error
}
}
[...]
Why can't I call clazz.method()?

Oliver Wong said:
You've been told why you can't call clazz.method(), but I'm a bit
concerned here about your design, as it implies you may have a
misunderstanding of how method overriding works. You don't need to use
generics or reflection or anything like that to call the static method. You
could just call it like this:

public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
x.method();
}
}

If you've defined another static method in class Y called "method",
thinking that it will somehow override the static method defined in class X,
you're mistaken. See
http://java.sun.com/developer/onlineTraining/new2java/supplements/2003/
Jun03.html#4

Ah, of course, you're right. The methods I'm calling is within Apache Torque,
but I guess method() is not the method I want. I'll have to look deeper
into the
documentation.
Thanks for pointing this out!
 
V

Vidar S. Ramdal

Vidar S. Ramdal said:
[...]
public class Tests extends TestCase {
public void testMethod(Class<? extends X> clazz) {
clazz.method(); // Compile error
}
}
[...]
Why can't I call clazz.method()?

Oliver Wong said:
[...]
If you've defined another static method in class Y called "method",
thinking that it will somehow override the static method defined in class X,
you're mistaken. See
http://java.sun.com/developer/onlineTraining/new2java/supplements/2003/

Ah, of course, you're right. The methods I'm calling is within Apache Torque,
but I guess method() is not the method I want. I'll have to look deeper
into the
documentation.
Thanks for pointing this out!

Now I see what happens here. method() is implemented in Class X, but,
for some reason, there's also a static method called method() in all
classes that extends X.
That's what confused me.
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top