Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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 if static is removed from main method?

0 Answers  


What is JVM and is it platform independent?

0 Answers  


What is difference overloading and overriding?

0 Answers   Cyient,


What is scope & storage allocation of global and extern variables? Explain with an example

0 Answers   IBS,


How do you access command-line arguments within the code?

0 Answers   Flextronics, Hexaware,


What is difference between next () and nextline () in java?

0 Answers  


Why are inner classes required?

3 Answers   Oracle,


When is finally block not called?

0 Answers  


Which way a developer should use for creating thread, i.e. Sub classing thread or implementing runnable.

0 Answers  


Can we have multiple public classes in a java source file?

0 Answers  


Difference between collection, collection and collections in java?

0 Answers  


what are depricated methods in threads and explain the lifecycle methods

5 Answers   CTS,


Categories