0

This thing come to my mind when I implemented custom Marker Interface. As the Serializable Marker interface is used for Serialization in java, I made my own Marker Interface to set a flag at class level.

public class MIOne implements Serializable,MarkerInterface{
    private int one;
    private String str;
    public MIOne() {
        super();
        this.one = 1;
        this.str = "MIOne";
    }

    Object writeObject() throws IOException {
        if (!(this instanceof MarkerInterface)) {
            FileOutputStream out = new FileOutputStream("D:\\testTransients.ser"); 
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(this);          
        } else {
            System.out.println("Unable to Support Searialization");
        }
        return null;
      }

    Object readObject() throws IOException, ClassNotFoundException {
        if (!(this instanceof MarkerInterface)) {
            FileInputStream fis = new FileInputStream("D:\\testTransients.ser"); 
            ObjectInputStream ois = new ObjectInputStream(fis); 
            MIOne c = (MIOne) ois.readObject(); 
        } else {
            System.out.println("Unable to Support Searialization");
        }
        return null;       
    }   
} 

But in this I have to make a blank Interface, than implement my logic accordingly (using instanceOf operator). I need your help to sort the same problem with some other better and simpler solution. Is that possible?

Prashant
  • 692
  • 2
  • 11
  • 26
  • 2
    Why do you want some other solution? For representing metadata there are two methods Markers and Annotations. There is no third way AFAIK. – Narendra Pathai Jun 25 '13 at 09:29
  • http://stackoverflow.com/questions/1995198/what-is-the-use-of-marker-interfaces-in-java see this question – Narendra Pathai Jun 25 '13 at 09:32
  • Like if in Parent class we have a field say marker which can be accessed and by that we can get which kind of marker we have. like this if any other solution which remove overhead of making blank interface and other logic implementation – Prashant Jun 25 '13 at 10:03

0 Answers0