Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP - dynamic creation of classes

 
Phil Lesh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to dynamically create classes. I have an interface called Algorithm in package "App.algorithms". There are algorithms in the same package implementing the interface. From a different package called "App.gui" I want to create a new object/instance of the algorithm with the name className. Here is part of the code:

I've tried setting the className to "App.algorithms.DijkstraShortestPath" and "DijkstraShortestPath" explicitly (where DijkstraShortestPath.java is a class in "App.algorithms") but it still fails on the second line. Am I missing something? Maybe I need to do something with ClassLoader? Please can you provide a bit of code. Thanks in advance.
Phil
 
Phil Lesh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
The exception that the above code throws is the InstantiationException. How would I instantiate the class within the above code? Thanks again!
Phil
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the javadocs for newInstance ...
Throws:
IllegalAccessException - if the class or its nullary constructor is not accessible.
InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
ExceptionInInitializerError - if the initialization provoked by this method fails.
SecurityException - if there is no permission to create a new instance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nullary is a new word to me. Get thee to a nullary? Does that just mean you have to have a no-parameter constructor? By the way, you have to have a no-parameter constructor.
If you must use a constructor with parameters, you have to get into reflection, and invoke it. Here's some code I have around:

See the whole project at Jim's Pages - Ooops. That zip is gone awol. I will try to remember to upload it at home tonite.
[ April 08, 2003: Message edited by: Stan James ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't a nullary a school where pointers go to learn how to be null? Kind of like a nunary...
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of which means that you have to create an instance of one of the classes that implements the Algorithm interface and then cast it to the Algorithm type.
You can not create an instance of an interface directly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic