0

I am using this singleton class in Java and in one method, I need an object of a class which gets instantiated in Main. I am not knowing how to pass that object to this method because this code is written in the constructor of the singleton class as I need it to be executed as soon as the program starts.

Should I take out the code from the constructor and make it a standalone method which I call from Main (though I wouldn't prefer this) or is there another way?

Any ideas?

Code:

Main:

public static void main(String[] args) {
    X x; // This is the object I need to pass to the singleton class
}

Singleton class:

public SomeSingletonClass {

    private Queue<Y> someQueue; // Y is another class I have in my project
    private SomeSingletonClass(){
        someQueue.add(new Y(<some data>, <some data>, <here I need an object of X as the constructor needs it>);
    }
}

I haven't added the entire code. Just a fragment where I am stuck.

pratnala
  • 3,723
  • 7
  • 35
  • 58
  • 3
    Can you share the code, please ? – Konstantin Yovkov Mar 05 '14 at 09:25
  • Indeed, it's very hard to *picture* code from a description - whereas if we've got a *sample* we can follow, we're much more likely to be able to help you. (As an aside, I'd generally try to avoid singletons anyway, but...) – Jon Skeet Mar 05 '14 at 09:26
  • I am adding some code. (As an aside, what's the issue with singletons?) – pratnala Mar 05 '14 at 09:29
  • Hope this would help you out http://stackoverflow.com/questions/1050991/singleton-with-arguments-in-java – Amila Iddamalgoda Mar 05 '14 at 09:29
  • @pratnala [Some people think singleton is stupid.](https://sites.google.com/site/steveyegge2/singleton-considered-stupid) – Radiodef Mar 05 '14 at 09:30
  • Sounds like you need to wrap the code in your singleton constructor in another method, so that it can be called by the constructor and also client code – Simon Bosley Mar 05 '14 at 09:30

1 Answers1

1

You have two main options.

The first will produce howls of derision - and rightly so because it is a dark tunnel of hell.

public class X {

}

public class Y {

    public Y(String s, X x) {

    }
}

public class Main {

    public static X x = new X();
}

public class SomeSingletonClass {

    private Queue<Y> someQueue = new LinkedList<>();;

    private SomeSingletonClass() {
        someQueue.add(new Y("Hello", Main.x));
    }

}

Here we make the X created by Main a public static so it is now, essentially, global state in parallel with your singleton.

Most readers will understand how nasty this is but it is the simplest solution and therefore often the one taken.

The second option is lazy construction.

public class BetterSingletonClass {
    private BetterSingletonClass me = null;
    private Queue<Y> someQueue = new LinkedList<>();

    private BetterSingletonClass(X x) {
        someQueue.add(new Y("Hello", x));
    }

    public BetterSingletonClass getInstance (X x) {
        if ( me == null ) {
            me = new BetterSingletonClass(x);
        }
        return me;
    }

}

Note that I have made no effort to make this a real singleton, n'or is this thread-safe. You can search for thread safe singleton elsewhere for plenty of examples.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • In the second one, is it even a singleton class any more now hehe? Anyway, thanks for the help. I probably have to read factories or something at this point or just separate my code out from the constructor. Both look like tunnels of hell to me lol – pratnala Mar 05 '14 at 10:10