• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Problem while playing with Reflection

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

I was just playing with Reflection and got into trouble.

I have two classes Class A and Class B.

Class A Code:
=============


Class B Code:
=============


Then i tried to run Class B it resulted in exception


java.lang.IllegalAccessException: Class B can not access a member of class A with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:349)
at java.lang.Class.newInstance(Class.java:308)
at B.doOperation(B.java:27)
at B.main(B.java:43)


Then i changed the code on both the class as follows

Class A Code:
=============


Class B Code:
=============



This code still gives Exception

java.lang.IllegalAccessException: Class B can not access a member of class A with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:349)
at java.lang.Class.newInstance(Class.java:308)
at B.doOperation(B.java:27)
at B.main(B.java:43)


I just want to call callThis() method in Class A dynamically without changing that private constructor on Class A. Where i am going wrong
Any help please...
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surely, you just don't need this line: -


Incidentally, there is no need to use Class.forName() to get the Class object of a class that's available at compile-time. Just use the "class literal". For instance: -


You may be able to use Class<Integer> here - I dunno without trying, which I don't have time for right now. If Class<Integer> does work, it's better code.
[ April 24, 2008: Message edited by: Peter Chase ]
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter Chase.

I have changed the things which you have quoted and it works as intended. Happy with the way i am learning

 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this horse might already be out of the barn, but just for future reference I think we ought to slide this over to the intermediate forum, I mean really... reflection?
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I just want to change my class A code to my first implementation

Class A:
========



I studied in a article that we can dynamically call a method which has been declared as private by overriding java language access checking.

(i.e.,) by using setAccessible(boolean) method in AccessibleObject class in reflection.

Likewise, is there a way i could use some method to get reference for the Singleton Class A which i have shown above.

Waiting for your answers!!!
 
Sheriff
Posts: 22716
129
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
By calling the private constructor, you're ensuring class A is NOT a singleton class!

If you want it to be a singleton class, all you need to do is have a static reference and a method to get the single object:
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all sorry for this late reply.
Rob thanks for your reply,

But, here my problem is that i will be getting the class name "A" during runtime only. So the reference for A cannot able to call getInstance() method directly because of the presence of private constructor. In that case what can i do?
 
Rob Spoor
Sheriff
Posts: 22716
129
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
If class A does not have a non-private constructor and does not have a (static) factory method to create objects of type A, then either class A is flawed or it is not meant to be instantiated.

Do you have the API for class A? If so, is there a factory method?
If so, you can use reflection to call that method:
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob, Now i have changed the code to call callThis method by following your implementation.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic