We help IT Professionals succeed at work.

Passing name of the class to method of other object as argument

pret32
pret32 asked
on
Medium Priority
184 Views
Last Modified: 2012-05-05
How to pass the name of the class, that I have in String variable, to the method of other class/object as an argument ?
e.g.

class Insert;

String s="Insert";

Action a.choose(??s??); // how this could be carried out

In other words, how to say one object which class to instantiate?

And please tell me how it is with functions.
Comment
Watch Question

Commented:
hi
Why do u want to pass it as string is that a restriction
if it is not what you could do is Create a method
in the to which u want to pass the class name

methodname(classname classn){
}

Hope its Clear...

regards Sumeet

Commented:
As a normal String :

//************************
package com.example;
class a {
}
//************************
class b {
  public Object instantiate(String className) {
    // you should be carefull at 'className' to contain, if appropiate, the full name, including the package.
    Object instance = null;
    Class loadedClass = null;
    try {
      loadedClass = Class.forName("de.sag.jdbc.adabasd.ADriver");
    } catch (ClassNotFoundException e) {}
    if(loadedClass != null)
      instance = loadedClass.newInstance();
    return(instance);
  }
}
//************************
class c {
.............
b instantiator = new b();
Object instantiatedClassA = b.instantiate("com.example.a");
System.out.println(((instantiateClassA != null)?"Instantiation succesful finished":"Instantiation unsuccesful finished"));
.............
}

Author

Commented:
i have the name of the class in string,this is a must. maybe i could convert it somehow?
Ovi

Commented:
Did you read the proposed answear ? I don't know if you have posted your comment before or after it.
Ovi

Commented:
... is a little mistake in the class c :

class c {
.............
b instantiator = new b();
Object instantiatedClassA = instantiator.instantiate("com.example.a");
System.out.println(((instantiateClassA != null)?"Instantiation succesful finished":"Instantiation unsuccesful
finished"));
.............
}

Ovi

Commented:
... and perhaps you should include the instantiation code into the try/catch statement like :
//************************
class b {
 public Object instantiate(String className) {
   // you should be carefull at 'className' to contain, if appropiate, the full name, including the
   //package.
   Object instance = null;
   Class loadedClass = null;
   try {
     loadedClass = Class.forName("de.sag.jdbc.adabasd.ADriver");
     if(loadedClass != null)
       instance = loadedClass.newInstance();
   } catch (Exception e) {}
   return(instance);
 }
}
//************************
Ovi

Commented:
... other update :
//************************
class b {
public Object instantiate(String className) {
  // you should be carefull at 'className' to contain, if appropiate, the full name, including the

  //package.
  Object instance = null;
  Class loadedClass = null;
  try {
    loadedClass = Class.forName("className");
    if(loadedClass != null)
      instance = loadedClass.newInstance();
  } catch (Exception e) {}
  return(instance);
}
}
//************************

Author

Commented:
I am this in the instatiation in the class Actions:
 try {
        loadedClass = Class.forName("Actions."+ActionName);
        if(loadedClass != null)
          instance = (Action)loadedClass.newInstance();
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        } catch (ClassNotFoundException e) {}
      return(instance);

Then in the POST method of servlet:
Actions as=new Actions;
Action a=as.GetAction(action);
a.Execute(res);

It all compiles successfully, but after execution, the java.lang.NoSuchMethodError comes. And it points to the line a.Execute(res); The Execute method comes from Class that is a subclass of Action. Action is an abstract class with a abstract Execute(...) method

What could be a problem?

Author

Commented:
Now I have different problem. How to declare 10-element array of String pairs?

String array[][]=new String[10][2];

array[2][0]
array[2][1]

???
Ovi

Commented:
array[0][0]
array[0][1]
array[1][0]
array[1][1]
array[2][0]
array[2][1]
...........

Explore More ContentExplore courses, solutions, and other research materials related to this topic.