Results 1 to 6 of 6

Thread: Serializable Singleton Implementation in Java

  1. #1
    Join Date
    Jan 2009
    Posts
    66

    Serializable Singleton Implementation in Java

    Hello, I am searching for the help from which I can able to get the Serializable Singleton Implementation in Java. I have tried it many times and also search this on different sources, but I am not able to get the solution for it. If anyone knows how to achieve it or how can we able to do it, then please reply me about it. I will be thankful to you.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Serializable Singleton Implementation in Java

    Hello, if you want to achieve the Serializable Singleton in Java then it is necessary to make use of the code below:
    Code:
    public class Singleton implements Serializable 
    { 
    static Singleton obj = new Singleton(); 
    private Singleton() 
    { 
    } 
    protected Object readResolve() 
    {
     return obj; 
    } 
    }
    So, just use the code and solve your problem.

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Serializable Singleton Implementation in Java

    Hello, if you want to use the Serializable Singeton in java then it is necessary to make use of the Singleton class object in your program. For getting this done you just need to implement a java.io.Serializable interface. And after implementing it you just need to use the method of the Serializable interface that is readResolve(). Which will be helpful to you for returning the object and throwing the ObjectStreamException. So, just create a program based on this and get your problem solved.

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Serializable Singleton Implementation in Java

    Hello, I have got the code below which you can use for your purpose. So, just make use of it and solve your problem.
    Code:
    import java.io.*;
    
    public class Serializable implements java.io.Serializable 
    {  
       static Serializable SSingleton;
       protected Serializable() 
      {
        }
          private Object readResolve() 
      {
               return instancd;
      }
      public static void main(String args[])
        {
          SSingleton = new Serializable();
          SSingleton.readResolve();
        }
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Serializable Singleton Implementation in Java

    Hey, I am not having more knowledge about it, But I have got the code below which will perform the serializable singleton Implementation in java.
    Code:
    public class Testing 
    {
     private static instance =  new Testing();
    
     private Testing() 
    {
      // construct object . . .
     }
    
     public static MySingleton getInstance() 
    {
      instance;
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: Serializable Singleton Implementation in Java

    Hello, I have got the code which is totally depending on the Serializable Singleton Implementation in Java. If you make use of it, then it will provide you more details about it. So, just read and understand it.

    Code:
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.ObjectInputStream;
    import java.io.arr;
    import java.io.Serializable;
    
    public class Testing implements Serializable 
    {
    private static final arr[] serialPersistentFields = 
    {
    new arr("obj", Testing.class),
    new arr("str", String.class)
    };
    private static Testing obj = null;
    private String str = null;
    private Testing() 
    {
    System.out.println("Constructing Testing");
    }
    private Testing(boolean f) 
    {
    System.out.println("Constructing Testing");
    System.out.println("Setting str to " +
    System.getProperty("abc"));
    str = System.getProperty("abc");
    }
    public String str() 
    {
     return str; 
    }
    public static Testing obj() 
    {
    if (obj == null) 
    {
    obj = new Testing(true);
    }
    return obj;
    }
    private void Object(ObjectOutputStream s) throws IOException 
    {
    ObjectOutputStream.PutField fields = s.putFields();
    fields.put("obj", obj);
    fields.put("str", str);
    s.writeFields();
    }
    private void readObject(ObjectInputStream objectinputstream) throws IOException 
    {
    try {
    ObjectInputStream.GetField fields = s.readFields();
    obj = (Testing) fields.get("obj", null);
    str = (String) fields.get("str", null);
    } 
    catch (ClassNotFoundException e) 
    {
    throw new IOException("" + e);
    }
    }
    }

Similar Threads

  1. Exception Handling in Singleton
    By Bottlenecked in forum Software Development
    Replies: 6
    Last Post: 20-09-2010, 09:14 PM
  2. Single instance of singleton for all JVMs
    By KANAN14 in forum Software Development
    Replies: 3
    Last Post: 30-10-2009, 07:10 PM
  3. Is IDictionary serializable ?
    By SmokiN in forum Software Development
    Replies: 3
    Last Post: 27-07-2009, 12:56 PM
  4. Singleton in AS2
    By Allen young in forum Software Development
    Replies: 3
    Last Post: 24-06-2009, 10:14 AM
  5. What is a Singleton in java?
    By Bhim in forum Software Development
    Replies: 3
    Last Post: 02-03-2009, 08:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,892,363.05232 seconds with 16 queries