Also you can use an Object's method to retrieve the class name of the object.
Arraylist list= null;
list.add(new MachineGun());
list.add(new Gun());
..
..
if(list.get(0).getClass().getSimpleName().equals("MachineGun")){
//here you can safely cast in the right class
MachineGun...
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.