Oracle Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

volatile boolean created;
SingletonObject object;
getInstance()
{
if(object == null)
{
synchronized(obj)
{
//Make sure object is not created already
if(object == null && !creating)
{
object = new SingletonObject();
created = false;
}
//Note this is a DCL implementation. This prevents from creating the object twice by two different threads.
}
}
else
{
return object;
}
}

- JDev March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use enumarator

- Anonymous March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Jdev is using double locked mechanism for thread safe singleton object creation. However, compiler is free to assign value to 'object' variable before 'object' is initialized.
For instance, thread1 can create an instance of SingletonObject class but before it initializes it can preempted. So another thread2 can still find 'object' == null and try to create another instance of SingletonObject class.
To avoid that easiest way is to use synchronized method instead of block
Or we can create public member of class which is initialized when first time class is used.
public class SingletonObject{
public final static object = new SingletonObject();
private SingletonObject(){
// To avoid misuse of constructor
}
}

- nil_dream March 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

synchronized method do work for sure. But again the question is what is the best way. Every time acquiring a lock for getting an object is leads to performance issue when heavily loaded.
Creating Object in advance works well. But then what if the object is never called. And probably this is not what interviewer was looking for.
This is a cleaner version of the earlier code.
volatile SingletonObject object;
getInstance()
{
if(object == null)
{
synchronized(obj)
{
//Only one thread can come here
//Make sure object is not created already. Object is volatile. Ensures that the JVM will see the correct copy of object.
if(object == null )
{
object = new SingletonObject();
}
//Note this is a DCL implementation. This prevents from creating the object twice by two different threads.
}
}
return object;
}

- JDev March 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create a static singleton instance at the beginning.
class Singleton {
private static Singleton obj = new Singleton();

private Singleton(){}

public static Singleton getinstance(){
return obj;
}
}

- Shekhar December 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Although as mentioned by several others the best and easiest way to ensure singleton behaviour would be by instantiating the class into the variable declaration, sometimes when the Singleton object has a lot of overhead and may or may not be used in your application, you should use lazy load and the best way to do it is by exploiting the 'volatile' keyword which ensures that the JVM supplies the latest version of Singleton within the Synchronized code.

public class Singleton {	

	private static volatile Singleton s;
	
	private Singleton()
	{
		
	}
	
	public static Singleton getInstance()
	{
		if(s == null)
		{
			synchronized(Singleton.class)
			{
			    if(s==null)
				{
				s = new Singleton();
				}
			}
		}
		
		return s;
	}
}

- teli.vaibhav May 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is an elegant implementation in Java with thread safety and lazy loading.

package com.journaldev.singleton;

public class BillPughSingleton {

    private BillPughSingleton(){}
    
    private static class SingletonHelper{
        private static final BillPughSingleton INSTANCE = new BillPughSingleton();
    }
    
    public static BillPughSingleton getInstance(){
        return SingletonHelper.INSTANCE;
    }
}

- vaibhav December 24, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More