Before starting your job search, look at these common Java interview questions asked during real interviews and understand the answers, but instead of memorizing the answers try to complete further research and come with your own personal solution for the interview question. The more prepared you are for what you will encounter, the more successful you will be. This collection of Java interview question could be also useful for software developers who need to interview Java developer.

Java Interview Question 8: When to use private constructor?

Usually Java interview questions for developers with 2 year experience, 3 year experience do not include basic questions like explaining difference between JDK and JRE or naming four access level modifiers in Java. I bet every beginner Java developer could name them as public, protected, default and private and most likely would remember that a constructor can use all four access modifiers. In the same time I would not except a beginner to answer on one of the must know Java interview questions after 2 years of experience - when to use private constructor? I hope to hear the following answer:
  • Private constructor is used if Java software developer does not want other classes to instantiate the object and to prevent class inheritance.
  • The instantiation could be done by providing a public static method, as is done when using Singleton Design Pattern or Factory Method Pattern.

No comments: