Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing Singleton class with Custom Class Loaders

 
sriram sundararajan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am trying to develop a custom class loader to load a singleton class.

Below are the code snippets involved.

I compiled Singleton1.java and got the class file in the same folder.

When i am trying to run ChkSingletonCL.java, i am getting a java.lang.IllegalAccessException
@ line 45. How can I run the Singleton class with custom class loader.

The Error msg. is "java.lang.IllegalAccessException: Class sripy.classLoader.ChkSingletonCL can not access a member of class Singleton1 with modifiers "private"

Basically i want to test the Singleton behaviours in the custom class loaders.

Singleton1.java




ChkSingletonCL.java





regards
sri
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question too difficult for "beginning". Moving thread.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

first, your Singleton1 class doesn't have a getName() method, so you must declare it first before invoking it from the main().


Second, constructor Singleton() is private, therefore you cannot use Class.newInstance() method because it works only for public default constructor.
Third, method getName() is private, so you cannot use class.getMethod(name,...) method because it works only for public methods.
See javadoc for details.

Try this code:
 
sriram sundararajan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ireneusz,
Thanks i can now able to call the singleton class. I think its high time for me to tune up my reflection knowledge. Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic