Re: Indirect references to classes?

From: Hal Vaughan (hal_at_thresholddigital.com)
Date: 10/12/03


Date: Sat, 11 Oct 2003 23:54:41 GMT

Thanks for both responses. I had no idea where to look or under what topic
to search. Both posts gave me info on classes I had not learned about yet.
It also showed my reference book (Java 2 in Plain English, by Overland and
Morrison, published by M & T Books) is behind, since the class Class is not
in it at all (this books claims to be a full reference book to Java classes
and API).

I came up with two methods (acutally an overloaded method) to act as a
constructor for a class where I only have the name, or name and parameters.
They're pasted in below, along with sample calling code:

static Object getObject(String className) {
        Object object = null;
        try {
                Class classDefinition = Class.forName(className);
                object = classDefinition.newInstance();
        } catch (Exception e) {System.out.println("Error w/ constructor: " + e);}
        return object;
}

static Object getObject(String className, Object[] args) {
        Object object = null;
        Class[] argTypes = new Class[args.length];
        for (int i = 0; i < args.length; i++) {
                argTypes[i] = args[i].getClass();
        }
        try {
                Class classDefinition = Class.forName(className);
                Constructor classConstructor = classDefinition.getConstructor(argTypes);
                object = classConstructor.newInstance(args);
        } catch (Exception e) {System.out.println("Error w/ constructor[]: " + e);}
        return object;
}

Be sure to add

import java.lang.reflect.Constructor;
import java.lang.Object;

at the start of the class you put these in.

For calling, if the constructor takes no args, call like this:

ClassName myObject = (ClassName) getObject("NameOfClassIWant");

For calling a constructor that takes args, call like this:

Object[] myParms = new Object[] {list, of, parameter, objects};
ClassName myObject = (ClassName) getObject("NameOfClassIWant", myParms);

Once you have myObject, you can use and abuse it like any other object.

Thanks, guys for the info.

I hope this helps a few others out there, since it provides a working way to
do this.

Hal

Hal Vaughan wrote:

> I'm not sure what the correct name for this would be. I'd think it's
> either an indirect reference, or a pointer, or something like that.
>
> I'm working on a program that would call a series of classes in different
> orders and there are times where I may add extra classes that weren't part
> of the original design. I'd like to be able to store a list of the
> classes, in the order that they are used, in a text file. Then I could
> have the first class read in the text file and call the classes in order
> as
> it goes through the text file. I don't want to have to hardcode a case
> statement, calling each class if the line matches the name, since I want
> to be able to easily add more classes in the future without re-compiling
> or altering the original class.
>
> If I have a classname stored in a String, is there any way to call that
> class from the name stored in the string?
>
> I know it can be done in Perl (I forget what it's called -- I think it's
> "dereferencing"), like this:
>
> &$function
>
> and it will call the function that matches the name stored in $function.
>
> Can I do something like this in Java?
>
> Thanks!
>
> Hal



Relevant Pages

  • Getting calling className in Java.Util.Logging.Logger
    ... I'm using Java 1.5 and I'm using Java's Logger class for logging. ... calling method for logging. ... contain the classname and method name. ... name and class name everytime to the log method. ...
    (comp.lang.java.programmer)
  • Re: invoke
    ... Here is the solution, ouf! ... Note that the returnObject would be different for a class having a ... static Object createObject(String className) { ...
    (comp.lang.java.help)