Accessing a method of one class in another class
Hi,
I am trying to access the method of one class in another class through an object of the former class.
Constraint is that i have the class name of the first class in a string variable.
i tried the following code
but i am unable to access the method..
Code :
package experiment;
public class t1 {
public void preent(){
System.out.print("test works");
}
}
--------------------------------------------------
Code :
package experiment;
import java.lang.reflect.*;
public class maiin {
static Object createObject(String className) {
Object object = null;
try {
Class classDefinition = Class.forName(className);
object = classDefinition.newInstance();
} catch (InstantiationException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
System.out.println(e);
}
return object;
}
public static void main(String[]args) throws ClassNotFoundException{
maiin M = new maiin();
Object O = M.createObject("t1");
O.preent();
}}
:-s help me
Thanks in advance 8->
Re: Accessing a method of one class in another class
preent isn't a method of type Object, you must type cast because the object has been polymorphed.
Code :
maiin M = new maiin();
Object O = M.createObject("t1");
if (O instanceof t1)
{
// just making sure it's a safe cast
((t1) O).preent();
}
Re: Accessing a method of one class in another class
thanks helloworld922...that does work :-bd
but i am encountered with this exception always...which is not allowing me from creating the object
Severity and Description Path Resource Location Creation Time Id
Class is a raw type. References to generic type Class<T> should be parameterized experiment/src/experiment maiin.java line 11 1269321729460 221
can u suggest me any thing regarding this
Re: Accessing a method of one class in another class
The class Class has a generic type. You can use a wild-card because you don't know what type it's suppose to be.
Code :
Class<?> classDefinition = Class.forName(className);
Re: Accessing a method of one class in another class
I have tried
Class<?> classDefinition = Class.forName(className);
but still its throwing an exception :confused:
Re: Accessing a method of one class in another class
but there is a problem..when i can use "t1" directly why will i store it in a string variable?...
the problem is i can just use the string(i.e classname) so , typecasting with t1 ( as in (t1) O) doesnt make sense in my context..
i can just use
Code :
t1 T = new t1();
T.preent();
instead which simplifies the job ...
i need something that just use the classname not the class :D
Re: Accessing a method of one class in another class
... It works for me.
Here's the full code I'm using:
Code :
public class Test
{
public static void main(String[] args)
{
String classname = "t1";
try
{
Class<?> test = Class.forName(classname);
Object o = test.newInstance();
if (o instanceof t1)
{
((t1) o).print();
}
}
catch (ClassNotFoundException exception)
{
// TODO Auto-generated catch block
exception.printStackTrace();
}
catch (InstantiationException exception)
{
// TODO Auto-generated catch block
exception.printStackTrace();
}
catch (IllegalAccessException exception)
{
// TODO Auto-generated catch block
exception.printStackTrace();
}
}
}
and the t1 class:
Code :
public class t1
{
public void print()
{
System.out.println("t1 works");
}
}