• 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

private constructor

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

How can access private constructor in outside the class ?

is any possible?

Thanks in advance
Mohan.M
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mohanasundaram muthukannan:
Hi,

How can access private constructor in outside the class ?

is any possible?



As the access modifer suggests, it is private and as such cannot be accessed from outside the class. It might be possible with reflection but I haven't tried it out myself.

Usually if you have a private constructor, there is typically a static method which allows you to obtain a reference. This is a classic Singleton pattern.

Is your question from purely an academic point of view or have you encountered a class with a private constructor and are unsure how to use it or are you planning to write a class yourself with a private constructor?
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think by private constructor it means you can not create an instance of the class from outside the class.

Only the static methods of the class can be accessed from outside the class as per

ClassName.methodName();

as the access modifier "private" says it is private for the class only,
it can not be accessed from outside the class.

with regards
S.Shekhar.
 
Ranch Hand
Posts: 83
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a sample code by which you can hack and call the private constructor of a class using reflection.







Here is the output of this program

I m hacked
 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the example.
 
Sachin Joshi
Ranch Hand
Posts: 83
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are few more similar hacking tricks based on same technique of setAccessible() method call.

Examples of
Object Hacking Java demonstrates how a Singleton can be accessed, how a private field can be accessed using same technique.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you access the private methods, instance variables also using this method?
Can you invoke private methods also?

And finally, what is the use of such mechanisms when we can access private methods, constructors and instances.
Dosen't it breach security?
[ September 02, 2008: Message edited by: Somnath Paul ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can you access the private methods, instance variables also using this method?


Yes.


Can you invoke private methods also?


Yes.


And finally, what is the use of such mechanisms when we can access private methods, constructors and instances.
Dosen't it breach security?


Access modifiers are not security mechanisms. If you genuinely don't want calls to particular methods you should secure them accordingly, don't rely on access modifiers.

Access modifiers are closer to guides to simplify APIs for other developers. Its not saying you really can't call this code, rather you really should be calling this code.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well said, but Sun could learn from this themselves:

The output:

If you know that there are also private fields int offset (the start in value), int count (the number of characters from offset to use) and int hash (the cached hash code, 0 means not cached) you can completely mutate Strings. Not so immutable anymore are they?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic