• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

HELP - dynamic creation of classes

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic