Comment

Comment by Tiomaidh

    public class Singleton {
        private static Singleton self = null;
        private Singleton() {
            //...
        }
        public static Singleton getInstance() {
            if(self == null) {
                self = new Singleton();
            }
            return self;
        }
        ...
    }
So at any point in your code you can call Singleton.getInstance() to have access to the same Singleton object. So it's effectively the same thing as a global variable.

Edit: Added `static' to be more correct.

parent Tiomaidh 15 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton