What happens when a Serializable is created using a Singleton template?

The question says it all. Are there any special events? I have a class that should implement the Serializable interface. I am developing an application using Wicket. I implemented a Factory class that will provide some references (to remove the redundancy of the same block of code written many times):

public class NavigationLinkFactory implements Serializable {

    private static final long serialVersionUID = 7836960726965363785L;

    private NavigationLinkFactory() {

    }

    private static class SingletonHolder { 
        public static final NavigationLinkFactory instance = new NavigationLinkFactory();
    }

    public static NavigationLinkFactory getFactory() {          
        return SingletonHolder.instance;

    }

    private Object readResolve() throws ObjectStreamException {
        return SingletonHolder.instance;
    }

    public Link<Serializable> getUserHomeLink() {
        return new Link<Serializable>("home", new Model<Serializable>()) {

            private static final long serialVersionUID = -8282608926361995521L;

            @Override
            public void onClick() {
                EMSSession session = (EMSSession) getSession();
                session.setActiveHorizonalMenu(1);
                setResponsePage(HomePage.class);
            }
        };      
    }

    public Link<Serializable> getProfileLink() {
        return getProfileViewLink();
    }

    public Link<Serializable> getProfileViewLink() {
        return new Link<Serializable>("profileView", new Model<Serializable>()) {

            private static final long serialVersionUID = 232185489384306999L;

            @Override
            public void onClick() {
                EMSSession session = (EMSSession) getSession();
                session.setActiveHorizonalMenu(2);
                setResponsePage(ProfileViewPage.class);
            }
        };  
    }
}

If I do not implement Serializable, then I get an exception in java.io.ObjectOutputStream.writeObjectthat is thrown by the wicket runtime framework. If I implement it, it will disappear.

So what happens when you call a method ObjectInputStream#readobject()on some object created by the Singleton pattern?

I am not adding a wicket tag because I do not think this question is related to Wicket.

+3
2

, , ( ) - . , : readResolve(), - case - . , - , singleton.

+5

. . . , , Java , JVM enum, , . , .

+3

All Articles