Vous êtes sur la page 1sur 2

Credit Suisse Interview Question for Analysts

0
of 0 votes

Answers

Implement a class using singleton pattern


- Singleton on November 12, 2008 Report Duplicate | Flag
Credit Suisse Analyst C

Email me when people comment.

0
of 0vote

ummm just look it up? public class Singleton { private static Singleton instance = null; private Singleton() {} public Singleton getInstance() { if (instance == null) instance = new Singleton(); return instance; } }
- Anonymous on November 14, 2008 | Flag

Reply to Comment

0
of 0vote

How to make it thread safe ?


- Amit on July 29, 2009 | Flag

Reply to Comment

0
of 0vote

get that static instance to initialise staticly instead of lazily... do

private static Singleton instance = new Singleton();

and the getInstance just returns 'instance'. That way you're removing all multithreading concerns from the singleton mechanism.
- Sam on October 11, 2009 | Flag

0
of 0 votes

What if there are other variables that need to be present in this class (which is usually the case) and need to be initialized in the constructor. In that case with what values will you initialize the singleton instance statically?
- Apurva on February 06, 2012 | Flag