Re: abstract static methods (again)
Tomas Mikula wrote:
On Tue, 20 Oct 2009 12:12:31 -0700, Daniel Pitts wrote:
Why do you want to enforce a static method to exist in children? I can
think of no good reason for it.
And I also want to enforce constructors. I provided two use-cases.
1. serialization frameworks. It is already required that a Serializable
class has a no-arg constructor. But this is not required at compile time.
You've said this a couple times, but are you sure it's true?
This class (with no no-arg constructor) appears to serialize and
deserialize just fine:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Serial implements Serializable {
private final int value;
Serial(int value) {
this.value = value;
}
public static void main(String[] unused)
throws IOException, ClassNotFoundException
{
Serial s1 = new Serial(42);
System.out.println("serializing: value = " + s1.value);
ByteArrayOutputStream outb = new ByteArrayOutputStream();
ObjectOutputStream outo = new ObjectOutputStream(outb);
outo.writeObject(s1);
outo.close();
ByteArrayInputStream inb =
new ByteArrayInputStream(outb.toByteArray());
ObjectInputStream ino = new ObjectInputStream(inb);
Serial s2 = (Serial) ino.readObject();
System.out.println("deserialized: value = " + s2.value);
System.out.println("they are "
+ (s1 == s2 ? "the same object" : "different objects"));
}
}
--
Eric.Sosman@sun.com