Re: What do use instead of overriding static methods?
Jim T wrote:
I'm extremely new to java, so I'm sure this has been asked before.
Please bear with me. Also please pardon any typos in my pseudocode.
Coming from Objective-C land, I have a class hierarchy and a bunch
of
methods that basically amount to this:
[snip]
I can't really follow this, but I think it menas "I can read some
token from the database that tells me what sort of object to create."
If so, in Java that would be the fully qualified class name, and you'd
call
Class.forName(fullyQualifiedClassName).newInstance()
While I'm at it, is there any way to dynamically choose a class to
call a class method upon? Again, in objective-C, you can do:
[[self class] someClassMethod];
So I'd think that the java equivalent would be:
this.getClass.someClassMethod();
But, alas, that doesn't seem to work either. Is this possible? And,
if
so, how?
You'd have to use reflection, which is generaly considered a bad idea
except when strictly necessary. I'll go with the usual: don't ask us
how to do "X" in Java, tell us what you're trying to accomplish. (OK,
I'll say one lower-level thing: if a method should be called via
virtual dispatch, make it an instance method whether it uses "this" or
not. Occastionally I've had to use the pattern
class Z extends Y
{
public method()
{
return method_static();
}
public static method_static()
{
...
}
}
which allows the method to be called either statically or via an
instance. It's always been when I'm generating code, so the
maintenance issue of having to keep this in sync across the hierarchy
hasn't been an issue.)