• Post Reply Bookmark Topic Watch Topic
  • New Topic

private constructor  RSS feed

 
mohanasundaram muthukannan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How can access private constructor in outside the class ?

is any possible?

Thanks in advance
Mohan.M
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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?
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the example.
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • 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 ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!