0

I have the class:

public class AbstractHandler {
    public static void handle(){ 
        throw new UnsupportedOperationException("Not implemented");
    }
}

which is going to have some number subclasses.

Also I have the following class which is going to use that class:

public class Consumer<T extends AbstractHandler>{

    public static void handle(){
        //I need to call the method 
        //T.handle() somehow
        //Is that possible?
    }
}
3
  • 1
    Why is #handle static anyway? Makes no sense to me, especially if you want polymorphism. Commented May 13, 2015 at 12:33
  • As stated by others - static is basically a violation of "OO concepts". One should only use it with great care; as it can make unit testing extremely hard. So besides the fact that your intended "usage" is plain wrong ... even it would somehow work: don't do it. Only make methods/objects static if there is no other way! Commented May 13, 2015 at 12:37
  • By the way, did you actually try calling T.handle();? It compiles and runs. Commented May 13, 2015 at 12:50

3 Answers 3

5

Since the method is static, there's no need to know what exactly T is.

Just do:

AbstractHandler.handle();
Sign up to request clarification or add additional context in comments.

Comments

2

Static methods are not inherited.

So you have AbstractHandler.handle, and that does not mean every T shall have one too.

If they do, it is not the same method either - there is no connection between then, even if they have the same name.

4 Comments

"that does not mean every T shall have one too" But you can call a static method using the subclass, right? class Sub extends AbstractHandler {} Sub.handle();
@Radiodef indeed you are correct, but only when Sub does not redefine handle; it's simply a syntax sugar as I see it, because if you do declare handle you can't add @Override annotation, can't call super.handle, et cetera, which suggests they aren't related :)
So then why can't T.handle() be the same as AbstractHandler.handle()? ; )
I'm guessing that's because T is erased at runtime, and therefore there would be no way to know whether handle would match a child that had handle or the parent's version - unlike if, for example, you had a T t object, whose class is not erased (though T is), and then the method chain could be fetched normally. But your argument is good, it would make sense if it were possible.
1

Since the method you plan to invoke is static, simply invoke:

AbstractHandler.handle();

... in your Consumer#handle method body.

I doubt this is actually what you want to do though.

You'd probably want to change the signature of handle as instance (i.e. not static), and invoke T.handle() in your Consumer#handle method body.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.