static method in super class
Suppose class A has many subclasses B, C, D,... (the names can be very irregular).
Is there a way to implement a mathod
public static newInstance(String xml)
in class A such that when I say
B.newInstance(anXml), the method in A will be invoked and returns an object of type B,
while C.newInstance(aValue) will invoke the same method but returns an object of type C?
Thanks for your help
Re: static method in super class
Have you tried writing a simple test case and see what happens?
Re: static method in super class
Quote:
Originally Posted by
Norm
Have you tried writing a simple test case and see what happens?
No. I don't know how. I used to program with ObjectC (cocoa), it's not hard doing such thing with
ObjectC. And I'd like to port code to Java
I heard that I need reflection functionalities in Java to do that.
Re: static method in super class
Why are you trying to use a static method?
Re: static method in super class
Quote:
Originally Posted by
Norm
Why are you trying to use a static method?
Because I don't assume an object of type B (C, D,...) exists. I think any method creating objects is static...
The reason I'd like to do things that way is that if I put newInstance method in each subclass, they look very similar and yet it's a big method, the resulting code will be very hard to maintain, and also ugly.
Re: static method in super class
Quote:
I think any method creating objects is static...
You should do some research on static in java. Here's a page in the tutorial.
Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Instances of a class are created with the new statement.