Guys, I have got a slightly different problem with Java Dynamic class loading. I have to pass an object( lets say Object A1 of class A) to the constructor of a different object B1 of class B, So that the A1's details are stored inside B1. B1 does not know what kind of object it is receiving. So all it knows is that A1 is an object.
Now I have functions in class B, which accept object C1 and check if it is equal to Object A1. So basically I have to check
- If object C1 is also of the type class A. ( Actually I am not past this point. )
- If the values inside the objects are the same
I tried a solution :
When storing the Object A1 inside B1 , I also store the name of the class "A" since object B1 only gets the object A1 but not the class name A.
public static void testing(Object C1, String s) //testing is the function of B called by B1
{
try{
Class c = Class.forName(s);
if( C1 instanceof c) // This is throwing error stating c cannot be resolved to a type
{
//further checking
}
}catch (Exception e){ System.out.println(e);}
}
I have been trying different code to get the class type to make a instanceof () comparision but I am not successful . Any suggestion ?
I even tried this but same error
ClassLoader classLoader = B.class.getClassLoader();
try{
Class class11 = classLoader.loadClass(s);
if ( C1 instanceof class11 )
{
}
}catch (Exception e){ System.out.println(e);}
Any pointer on how to proceed would be very helpful !
Solution is to use : isInstance instead of instanceof
There is also other issues attached with this problem . Let me update it with my below comment. I, now, have to compare a value , say AGE, in both C1 and A1, which inside B1. Now How do I do that ? If I try the functions or values , I will give me compiler error because the compiler is still oblivious of the class type of the object C1 and A1
Also I could have saved X1 instead of A1 in the first place. Now I might have to check for a different property check like ADDRESS. This changes the dimension of the problem drastically.