How to create an instance of a class if the class has
private constructor?

Answers were Sorted based on User's Feedback



How to create an instance of a class if the class has private constructor?..

Answer / eswar

u can create instance like this...

class Ex
{
public static Ex e=null;

private Ex()
{}

public Ex getObj()
{
if(e==null)
e=new Ex();
return e;
}
}

Actually the above coding is example for singleton Java
class.... which creates omly one object per JVM

Is This Answer Correct ?    6 Yes 1 No

How to create an instance of a class if the class has private constructor?..

Answer / krishnanv

The above answer is perfectly ok only one thing missing i.e
the getObj() method must be static, then only we can call
directly without instantiating that class object.

Is This Answer Correct ?    2 Yes 0 No

How to create an instance of a class if the class has private constructor?..

Answer / meera

create an instance of a class within the same class.
then u can create an instance of a class if the class has
private constructor.

Is This Answer Correct ?    3 Yes 2 No

Post New Answer

More Core Java Interview Questions

How can final class be used?

5 Answers   Accenture, Wipro,


Is java 1.7 the same as java 7?

0 Answers  


What is a java object and java application?

0 Answers  


What is lambda in java?

0 Answers  


How are Observer and Observable used?

3 Answers  






How do you create a bulleted list?

0 Answers  


What does substring mean?

0 Answers  


How many bytes are there?

0 Answers  


Is 0 an irrational number?

0 Answers  


Is it possible to write JAVA program without including any of the packages,such as "import java.io.*"; bcoz I instantly wrote a code without "import..." statement and runned the program on command Line & it worked. the code is: class Person { String name; int age; Person(String s,int a) { name = s; age = a; } accept() { System.out.println(name+"Hi!!!!!"); System.out.println(age); } } class Demo { public static void main(Strings args[])throws IOException { String name = args[0]; int age = Integer.parseInt(args[1]); Person p = new Person(name,age); p.accept(); } }

3 Answers  


what modifiers are used with top-level class?

2 Answers  


What are the uses of java?

0 Answers  


Categories