Define object of a class derived from a string variable -
following use case:
invoker class (with main method)
public class invoker { public static void main(string[] args) { string class_file="batch_status"; } }
class invoked (with same method name of class name, e.g. in case batch_status)
import java.util.*; public class batch_status { public static void batch_status(string args) { ...... ......code goes here ...... } }
now problem not able define object such test in invoker class using value of string class_file such class_file test = new class_file();
above snippet, in production code values in string variable vary , each value, different class file (the name of class file same of value of string variable).
please suggest.
regards
this code demonstrates being able retrieve class instance given string:
string classstring = "com.rbs.testing.test"; try { class c = class.forname(classstring); test test = (test) c.newinstance(); } catch (classnotfoundexception e) { e.printstacktrace(); } catch (instantiationexception e) { e.printstacktrace(); } catch (illegalaccessexception e) { e.printstacktrace(); }
if don't know class cast yet, can dump
c.newinstance();
into object class, use if else clauses until find out class type contained in object.
object o = c.newinstance(); if (o instanceof test) { } else if(o instanceof test2) {
i hope helps. sorry if misunderstood need.
Comments
Post a Comment