-1

I have superclass Token with some subclasses like Knight, King, Queen, etc.

I need a random Token Type so I call this method:

public Class randomTokenType(){
    Class[] classes = {
        Bishop.class, King.class, Knight.class, Pawn.class, Queen.class, Rook.class
    };
    Random random = new Random();
    return classes[random.nextInt(6)];
}

Class<Token> tokenType = randomTokenType();

Now I want to call a static method on this tokenType, for example:

tokenType.displayString()

The compiler can't resolve this method even tough it's implemented in Token and all of its subclasses.

What is my mistake?

SQB
  • 3,926
  • 2
  • 28
  • 49
jesper
  • 1
  • 3
  • What do you think the `Class` class is? Why do you think so? Polymorphism and `static` are contradictory in Java. – Sotirios Delimanolis Jan 08 '16 at 18:23
  • Also, please don't add javascript code snippet for Java code. Take a look at the help center for how to post code snippets correctly. – Sotirios Delimanolis Jan 08 '16 at 18:26
  • Im new in Java sorry. Normally i code in Smalltalk where even Classes are Objects. There´s no difference in behavior between "classes" and "objects" – jesper Jan 08 '16 at 18:27
  • Your question may in fact be an [XY Problem](http://mywiki.wooledge.org/XyProblem) where you ask "how do I fix this code problem" when the best solution is to use a different approach entirely. Consider telling us the overall problem that you're trying to solve rather than how you're currently trying to solve it. – Hovercraft Full Of Eels Jan 08 '16 at 18:44

2 Answers2

1

What you are actually looking for is reflection - see Invoking a static method using reflection

in you case that would be:

Method method = tokenType.getMethod("displayString");
method.invoke(null);

A Class-object is a sort of an index. It contains methods that allow you to query what the actual .class file contains (like its methods, fields, annotations et al).

You cannot access them directly (like an index points only to WHERE the information is - not the information itself) - instead you need to query the index with i.e. Class.getMethod("nameofMethod")

once you got the "pointer" to the method you can try to call it (via Method.invoke).

Depending on what kind of method it is, you need to pass the invoke method only null (for static methods) or an instance of the object (for non-static).

Reflection allows you to create such an instance on-the-fly as well.

For more information I suggest reading up on reflection and especially the javadoc of Class. It explains a lot.

Edit: this only works if the method displayString is declared like this:

public class Bishop{
    public static void displayString() {
        System.out.println("Bishop");
    }
}

public class Test {
 public static void main(String args[]) throws Exception {
    Class<?> tokenType = Bishop.class;
    Method method = tokenType.getMethod("displayString");
    method.invoke(null);
 }
}

if there are parameter or it is private, then this will not work

Community
  • 1
  • 1
Niko
  • 6,133
  • 2
  • 37
  • 49
  • Already tried. I get an NoSuchMethodException. Same with '.getDeclaredMethod' – jesper Jan 08 '16 at 18:32
  • the question is if the method is public. I just tried it with: public class Bishop{ public static void displayString() { System.out.println("Bishop"); } } – Niko Jan 08 '16 at 19:26
0

There are quite a few problems with your code. Few of them are

  • You are asking a class to return something.

    public Class randomTokenType() //dont know what this is supposed to mean ?

  • If you add static to a method definition that method can never be overrriden

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108