• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, the document says that Class.forName(String) needs a public zero argument constructor.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chaitanya karthikk wrote: . . . Class.forName(String) needs a public zero argument constructor.
Surely you mean the newInstance() method.
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a problem with using a static method? the API sure does it often enough. one i particularly like is BigInteger.valueOf(long)
 
Rishi Shah
Ranch Hand
Posts: 43
Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amen on that!
of course all java programmers add extra(unneeded) modifiers just to show off.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful, RT. One of these days somebody will believe you
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rishi Shah wrote:
This is not working
It prints 0. When I make it public the control is entering if condition.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chaitanya,

This code works now. I made a few changes.


 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Yes, Security manager doesnt allow this.and Thank you...
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic