I have an object, want to convert the generic object to class object. I thought the following should work. But it doesn’t:
Object o1=itr.next();
Class cls=o1.getClass();
Object obj=(cls) o1;
What can the error be? How can I do this correctly?
Update: Here i have the list of objects that belongs to different classes. I will be using each class in each loop to convert to XML or JSON string. for this i want to get the class and object it belongs.
You have to call the cast method on the Class object: