1) write a program that singleton objects returns two instances?

Give me answer

Showing Answers 1 - 8 of 8 Answers

V.Nalini

  • Sep 21st, 2005
 

public class snglton

{

public static void main(String args[])

{

snglton s=new snglton();

someotherclass s1=new snglton();

}

}

  Was this answer useful?  Yes

vinod

  • Sep 27th, 2005
 

write a singleton class and call object.clone() method on the object.

  Was this answer useful?  Yes

kuriakose

  • Sep 30th, 2005
 

public class CSingleton {
   private static CSingleton inst= null;
protected CSingleton() {
                               }
public static CSingleton getInstance() {
      if(inst == null) {
         inst = new CSingleton();
      }
      return instance;
   }
}
public class SingletonIns {
  public SingletonIns() { 
CSingleton inst = CSingleton.getInstance();
CSingleton anotherInst = new CSingleton();
}
}

swarna

  • Oct 14th, 2005
 

 we can't create more than one instance for singleton object.

  Was this answer useful?  Yes

Jagdeep Grewal

  • Nov 7th, 2005
 

public class CSingleton {
   private static CSingleton inst= null;

private static int count=0;
protected CSingleton() {

                        }
public static CSingleton getInstance() {
      if(count < 2) {

         count++;
         inst = new CSingleton();
      }
      return instance;
   }
}
public class SingletonIns {
  public SingletonIns() { 
CSingleton inst = CSingleton.getInstance();
CSingleton anotherInst = new CSingleton().getInstance();
}
}

  Was this answer useful?  Yes

class Singletones implements Serializable{
    /**
     *
     */
    private static final long serialVersionUID = 6240073482570369797L;
    private static Singletones instance;
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private Singletones(){
        
    }
    public static Singletones getInstance(){
        if(instance == null){
            instance = new Singletones();
        }
        return instance;
    }
}

public class Store {
    public static void main(String... arg) throws IOException, ClassNotFoundException, FileNotFoundException{
        Singletones obj1 = Singletones.getInstance();
        obj1.setName("name1");
        FileOutputStream file = new FileOutputStream("c:\singleton.ser");
        ObjectOutputStream oos = new ObjectOutputStream(file);
        oos.writeObject(obj1);
        FileInputStream file1 = new FileInputStream("c:\singleton.ser");
        ObjectInputStream ois = new ObjectInputStream(file1);
        Singletones obj2 = (Singletones) ois.readObject();
        obj2.setName("name2");
        System.out.println(obj1.getName());
        System.out.println(obj2.getName());
    }
}

Output:-
name1
name2

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions