• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, the document says that Class.forName(String) needs a public zero argument constructor.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chaitanya karthikk wrote: . . . Class.forName(String) needs a public zero argument constructor.

Surely you mean the newInstance() method.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 43
Mac Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
amen on that!
of course all java programmers add extra(unneeded) modifiers just to show off.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Careful, RT. One of these days somebody will believe you
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chaitanya,

This code works now. I made a few changes.


 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

Yes, Security manager doesnt allow this.and Thank you...
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic