Results 1 to 6 of 6

Thread: Program for singleton objects which returns two instances

  1. #1
    Join Date
    Dec 2009
    Posts
    41

    Program for singleton objects which returns two instances

    Hello Friend,

    I have to code one java program, in which singleton objects should returns two instances. I am getting troubled while coding this java program. I have one program code for this but it's not showing desired output. If anyone posses the java code in which the singleton objects returns two instances, then please share those java code with me. I will really appreciate you help.

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

    Re: Program for singleton objects which returns two instances

    Hi friend,

    You need to refer following in which a singleton object which returns two instances:
    Code:
    public class JsingletonDemo
     {
       private static JsingletonDemo inst= null;
    protected JsingletonDemo() {
                                   }
    public static JsingletonDemo getInstance() {
          if(inst == null) {
             inst = new JsingletonDemo();
          }
          return instance;
       }
    }
    public class SingletonIns1 {
      public SingletonIns1() { 
    JsingletonDemo inst = JsingletonDemo.getInstance();
    JsingletonDemo anotherInst = new JsingletonDemo();
    }
    }

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

    Re: Program for singleton objects which returns two instances

    Default constructor for Singleton is private, any other object in the system has to access the Singleton object through the Instance property. Separating the abstract concept from a particular implementation, as this patterns collection does, may make the Singleton implementation look surprisingly simple. See following example:
    Code:
    public class singltonP
    
    {
    
    public static void main(String argl[])
    
    {
    
    singltonP sh =new singltonP();
    
    someotherclass sh1=new singltonP();
    
    }
    
    }

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

    Re: Program for singleton objects which returns two instances

    Hi,

    In following java example the singleton object "anotherInst" returns two instances. I suggest you to copy this program and try to run in your system and see if it's executed successfully or not:
    Code:
    public class TestSingleton {
    private static TestSingleton inst null;
    protected TestSingleton() {
    }
    public static TestSingleton getInstance() {
    if(inst null) {
    inst new TestSingleton();
    }
    return instance;
    }
    }
    public class TSingletonIns {
    public TSingletonIns() {
    TestSingleton inst TestSingleton.getInstance();
    TestSingleton anotherInst = new TestSingleton ();
    }
    }

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Program for singleton objects which returns two instances

    Hello friend,

    I have following java program code, and which show how to define Singleton object which can returns two instances:
    Code:
    public class Sngt {
    private static Sngt inst null;
    
    private static int count 0;
    protected Sngt() {
    
    }
    public static Sngt getInstance() {
    if(count1 < 2) {
    
    count1++;
    inst new Sngt();
    }
    return instance;
    }
    }
    public class SingletonIns {
    public SingletonIns() {
    Sngt inst Sngt.getInstance();
    Sngt anotherInst new Sngt().getInstance();
    }
    }

  6. #6
    Join Date
    Dec 2009
    Posts
    41

    Re: Program for singleton objects which returns two instances

    Hello Friend,

    Thanks for all your instant help!!! All you suggestions was very help to code java program for singleton object which returns two objects. As in question I had told that my program code is not giving desired output. So I correct those code using your above mentioned code. And now this code is executed properly. I am appreciating your help.

Similar Threads

  1. Exception Handling in Singleton
    By Bottlenecked in forum Software Development
    Replies: 6
    Last Post: 20-09-2010, 09:14 PM
  2. Serializable Singleton Implementation in Java
    By Bhardwaj in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 07:17 PM
  3. Single instance of singleton for all JVMs
    By KANAN14 in forum Software Development
    Replies: 3
    Last Post: 30-10-2009, 07:10 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,718,150,393.13459 seconds with 16 queries