Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
01 public class MyClass{
02 private static MyClass instance = null;
03
04 private MyClass(){
05 }
06
07 public synchronized static MyClass getInstance(){
08 if ( instance == null ){
09 instance = new MyClass();
10 }
11
12 return instance;
13 }
14 }
{
public class Singleton {
private Singleton(){
System.out.println("I am Singletone - Private constructor");
}
private static class SingletonHolder{
public final static Singleton instance = new Singleton();
}
public static Singleton getInstance()
{
System.out.println("I am Singleton - getInstance()");
return SingletonHolder.instance;
}
}
Hi,Please help me how can I prepare for Amdocs written test for BTECH freshers.please mail me complete set of question/answers or how can I proceed for that at sagarr807@gmail.com.
I have only two days left for that.Please help me for the same.Also mail me Networking Interview question/Answers
Using the Synchronized is very costly.
public class Singleton {
private static Singleton uniqueInstance = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
System.out.println(uniqueInstance.hashCode());
return uniqueInstance;
}
}
public class TestSingleton {
public static void main(String[] args) {
Singleton.getInstance();
}
}
The easiest implementation consists of a private constructor and a field to hold its result, and a static accessor method with a name like getInstance().
The private field can be assigned from within a static initializer block or, more simply, using an initializer. The getInstance( ) method (which must be public) then simply returns this instance:
- Sunil B N May 02, 2013