Skip to main content
gamedev.net
🔒 Locked

[Java] Calling template's static methods

Started by Degra Mar 1, 2007 at 11:53 AM 1 replies 2.3k views
Original Post
Degra
Degra
I've got a class which looks something like this.. public class MyClass { } 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
Excors
Excors
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 MyClasss 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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.