Jump to content
  • Advertisement
Sign in to follow this  
Degra

[Java] Calling template's static methods

This topic is 4133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got a class which looks something like this.. public class MyClass<theSubClass extends theSuperClass> { } theSuperClass and theSubClass are completely static. I need to be able to call a method in theSubClass (overridden from theSuperClass) from a function in MyClass, like this.... public void myFunction() { theSubClass.aStaticMethod(); } but I can't get it to work. Has anyone got any ideas? I'm fairly new to Java so it's probably a lack of understanding on my part. Degra

Share this post


Link to post
Share on other sites
Advertisement
Unlike C++ templates, Java generics only compile a single copy of your methods, so there's only a single implementation of myFunction (whereas C++ does one version per instantiated type).

When you call a static method of a class, there is no object involved, so you can't do run-time polymorphism (because that's based on looking at an object's vtable) - it's essentially the same as a plain function call in C.

So the call to aStaticMethod is jumping to a static method that it found at compile-time, and the code which does that call is only existing once (regardless of how many MyClass<T>s you use anywhere in the program), so it can only ever jump to one location, which means it can't possibly depend on the type of theSubClass. You can only use static methods when you tell the compiler precisely which class to look in.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!