• Post Reply Bookmark Topic Watch Topic
  • New Topic

class Loading  RSS feed

 
Abi Raj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to load only one instance of a class in a separate thread. The class name will be known during runtime. The class will be a subclass of the class which loads it. Can anyone tell me how to implement this?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. What kind of project are you working on?
 
Abi Raj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Atleast I want to load the class only once. can I use singleton. But the problem is i dont know class name.. It will be known during run time only.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure ... I typed up a whole solution then remembered THIS IS THE RANCH and I should make you do the work ... I mean get the pleasure of discovery ...

How does a Singleton normally populate its single instance? Search the Ranch for "singleton" if you've never done one. There's the way Sun used to recommend, since proven broken, and some simpler ways.

How could you instantiate a class if you only knew its name? See the JavaDoc for Class newInstance() and forName() if you've never done one.

How could you get a classname at runtime? There are many answers to this one; Properties is one that might work for you.

Can you figure out those three puzzles and put them together? Try some code and give us a holler if you get stuck. Have fun!
 
Abi Raj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the part how to instantiate a class with a class name which is known only at run time. But the problem is I dont know how the class is implemented. but the class will be a subclass of the class loading it.. I want that class to be loaded only once may be in a separate thread.. If I use singleton the class has to implement getInstance and make the constructor as private. I may be wrong about singleton. Any Ideas.?? Here is my code.


public Class1(String cClassName)
{
Object objClass;
try
{
objClass = Class.forName(cClassName).newInstance();
}
catch (Exception exp)
{
exp.printStackTrace();
}

}
[ January 06, 2006: Message edited by: Abi Raj ]
 
Raghu Arikeri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by default newInstance() method will invoke the default constructor. As the constructors for Singleton are private, its thowing the following exception.

java.lang.IllegalAccessException: Class TestSingleton can not access a member of class SingletonThree with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
at java.lang.Class.newInstance0(Class.java:290)
at java.lang.Class.newInstance(Class.java:249)
at TestSingleton.main(TestSingleton.java:29)

is there any way by which the getInstance() method of the singleton can be accessed??
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the constructors for Singleton are private


This isn't necessarily so. One goal with singleton is to control creation, which is usually read as preventing ourselves from creating them.

A variation called "Just Create One" makes the constructor public but trusts future developers to know better. For example we could make a new Servlet class, but we don't.

Another variation might be to use a package to hide the concrete classes and expose only an interface and a factory.

But if you really like the private constructor, you could use reflection to get the static getInstance() method and execute it. All your subclasses could call a super method to make sure that only one instance is ever created even if you try two different subclasses. See Class.getDeclaredMethod() to start.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghu:

is there any way by which the getInstance() method of the singleton can be accessed??

Raghu, please review our naming policy and change your displayed name accordingly. Thanks.

Second, if the getInstance() method you describe isn't static, you got bigger problems. If it is, then call it after you load the class.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!