• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

how to import class from within another class

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please tell me how to import a class from
another class method.
I don't want the import javapackage.classname; format
i need to import a class file from a method in another class.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the complete packagename.classname in your method.

For e.g.
java.util.HashSet hashSet = new java.util.HashSet();
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's the situation:
i retrieve the path of the class to be imported
during the course of the program and now i need to use
the methods defined in that class..how do i achieve that?
 
Srikanth Basa
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by udhu:
here's the situation:
i retrieve the path of the class to be imported
during the course of the program and now i need to use
the methods defined in that class..how do i achieve that?



Can you be more clear ?

If I understand it properly, you mean you know the name of the package name and the Class name at the runtime and you want create an instance of this class and invoke a particular method on it. Am I correct ?

If what I understand is true then you must be using Java Reflections.
[ February 09, 2007: Message edited by: Srikanth Basavaraju ]
 
Srikanth Basa
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To use reflections in simple scenario without any complexities, its better to have an interface "MyInterface" and you wish to invoke a method named "myMethod" which is provided in interface
The class which you get to know at the runtime implements this interface.

-------------------------------------------------
// Sample snippet to give you a brief idea
// Note that I did not compile.

Class [] constructorParamerTypes = null;
// TODO Based on the constructors initialize "constructorParamerTypes" to an array of Classes

Object [] parameters = null;
// TODO Initialize the parameters to the Objects that you wish to pass into the constructor initialization

String className = null; // Class name is known at runtime
// TODO initialize className

Class myObject = Class.forName(className);

// Load the class that has been configured.
Constructor constr = myObject.getConstructor(constructorParamerTypes);

MyInterface instance = (MyInterface) constr.newInstance(parameters);

instance.myMethod();
-------------------------------------------------

Hope this helps
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello "udhu"-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, what else do you know about about the class, besides its name? If you don't at least know some interface it implements, it'll be hard to call its methods. How do you know which methods to invoke?

If you do know that it implements, e.g., "MyInterface" the code would be something like this:

 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help..
I took your advice and tried to publish the imported class methods using
the interface and i'm able to call the methods using the
interface instance.
I also looked up java reflections...I had not known about them
before..You do make us explore java in a fantastic way...
Thanks a ton...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic