aspose file tools*
The moose likes Java in General and the fly likes How to create an Object with private constructor without using a static method in the class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to create an Object with private constructor without using a static method in the class?" Watch "How to create an Object with private constructor without using a static method in the class?" New topic
Author

How to create an Object with private constructor without using a static method in the class?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi all, I would like to know how to create an object or a class with private constructor.

I know one such way. Creating a static method in the class which will create an object and return the object of the class. The singleton approach.

I think I can use Class.forName(String). But I am not pretty sure.

I hope reflection api also will help but not sure.

Can anyone please tell me what are the different ways to create an object whose class has a private Constructor?

Thank you all in advance. Good day.


Love all, trust a few, do wrong to none.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
If the constructor is marked private, you will receive IllegalAccessException while trying to create an instance using reflection. Read more on creating instances using reflection here.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

John Jai wrote:If the constructor is marked private, you will receive IllegalAccessException while trying to create an instance using reflection. Read more on creating instances using reflection here.

What about Class.forName(String). Is there any way to create an instance using this? I wrote this codeWhen I tried to execute it i got this errorSo now I hope it is not possible with Class.forName(String).

Am I right?
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Okay, the document says that Class.forName(String) needs a public zero argument constructor.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
chaitanya karthikk wrote: . . . Class.forName(String) needs a public zero argument constructor.
Surely you mean the newInstance() method.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

is there a problem with using a static method? the API sure does it often enough. one i particularly like is BigInteger.valueOf(long)


SCJP
Visit my download page
Rishi Shah
Ranch Hand

Joined: Sep 05, 2012
Posts: 43

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Beware of reflection; you are losing performance and that approach may introduce errors; the original designer wanted that constructor private for a reason.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

amen on that!
of course all java programmers add extra(unneeded) modifiers just to show off.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Careful, RT. One of these days somebody will believe you
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Rishi Shah wrote:
This is not working
It prints 0. When I make it public the control is entering if condition.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Rishi and chaitanya, both of you might do well to read the API for Class#getConstructors()
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.


luck, db
There are no new questions, but there may be new answers.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Darryl Burke wrote:Rishi and chaitanya, both of you might do well to read the API for Class#getConstructors()
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.
I was trying to tell Rishi that it is not possible.
Rishi Shah
Ranch Hand

Joined: Sep 05, 2012
Posts: 43

Darryl Burke wrote:Rishi and chaitanya, both of you might do well to read the API for Class#getConstructors()
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object.


Yeah, here's the right answer:

ktelagar krish
Greenhorn

Joined: Dec 06, 2012
Posts: 2
Hi Chaitanya,

This code works now. I made a few changes.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Yes, it works, but as previously suggested, setAccessible(true) is cheating. You can set a SecurityManager object which will prohibit that, but I am not sure how to do it.

And welcome to the Ranch
ktelagar krish
Greenhorn

Joined: Dec 06, 2012
Posts: 2
Hi Campbell,

Yes, Security manager doesnt allow this.and Thank you...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
You’re welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to create an Object with private constructor without using a static method in the class?