Interview Question for Java Developers


Country: India
Interview Type: In-Person




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

Short answer: If we want to maintain a single object of a class per JVM .. :)
Long answer: 1. If we have to maintain some information at single place or dont want to spreed everywhere in application, than we prefer singleton ...like objects for configurations, factories etc.
2. Only single object is enough of a particular functionality (class) like .... starters, initializers etc. etc.

- Ajeet August 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For "short" and "long" , IMO they are similar, both per JVM.
If need scale limitation, you can change public to default.
Or register a SecurityManager to limit specific invoking case.
Below is a Singleton example, you can also use Enum or Interface(java1.8+)

package gao.zone.study;

public class MySingleton {
	
	private final static class Holder{
		private final static MySingleton INSTANCE = new MySingleton();
	}
	
	private MySingleton(){}
	
	public final static MySingleton getInstance() {		
		return Holder.INSTANCE;
	}
	
	public void foo() {	}
	
	public <T> T bar() {return null;}
}

For 2. I think a private static class is enough.

- ttoommbb@126.com August 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

From the name it is clear that suppose we want only one instance of a class in JVM then we will use singleton pattern. One such example:

public class HibernateUtils
{
private HibernateUtils()
{

}

private static SessionFactory seesionFactory = null;

private static final SessionFactory makeSessionFactory()
{
try {
if (sessionFactory==null)
seesionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return seesionFactory;
}

public static SessionFactory getSessionFactory()
{
return makeSessionFactory();
}


}

- Mritunjay Kumar August 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A Singleton candidate must satisfy three requirements :-

1) Controls concurrent access to a shared resource.
2) Access to the resource will be requested from multiple, disparate parts of the system.
3) There can be only one object.

- saxenasaral August 29, 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