Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 18-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,848
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.
Reply With Quote
  #3  
Old 18-02-2010
Member
 
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.
Reply With Quote
  #4  
Old 18-02-2010
Member
 
Join Date: May 2008
Posts: 2,008
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();
    }
}
Reply With Quote
  #5  
Old 18-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
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;
}
Reply With Quote
  #6  
Old 18-02-2010
Member
 
Join Date: May 2008
Posts: 2,293
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);
}
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Serializable Singleton Implementation in Java"
Thread Thread Starter Forum Replies Last Post
Exception Handling in Singleton Bottlenecked Software Development 6 20-09-2010 09:14 PM
Single instance of singleton for all JVMs KANAN14 Software Development 3 30-10-2009 07:10 PM
Is IDictionary serializable ? SmokiN Software Development 3 27-07-2009 12:56 PM
Singleton in AS2 Allen young Software Development 3 24-06-2009 10:14 AM
What is a Singleton in java? Bhim Software Development 3 02-03-2009 08:18 PM


All times are GMT +5.5. The time now is 06:42 AM.