How to create an instance of a class if the class has
private constructor?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
How do you define a method?
Why non nested classes in java are not having marked as protected access specifier
What modifiers may be used with a top-level class?
What are the major advantages of internal iteration over external iteration?
What is the use of set in java?
What is protected and friendly?
What is :: operator in java 8?
What’s the difference between applets and standalone program?
What is the purpose of nested class in java?
How does class forname work in java?
What will happen when using pass by reference in java?
What is the use of keywords in java?