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

What is the simpletimezone class in java programming?

0 Answers  


Difference between == and .equals() ?

0 Answers  


Can java list contain duplicates?

0 Answers  


Which collection is sorted in java?

0 Answers  


why java uses class level type casting ?

0 Answers  






What are the differences between string, stringbuffer and stringbuilder?

0 Answers  


What is the difference between a constructor and a method?

0 Answers  


What is Mutex (Mutual Exclusion Object) ?

0 Answers   Ciena,


Explain the features of interfaces in java?

0 Answers  


What is quick sort in java?

0 Answers  


Explain listiterator and methods in listiterator?

0 Answers  


What is use of functional interface in java 8? Explain

0 Answers  


Categories