I’m thinking of implementing a ‘container’ with Java where I can store any type of objects, Integer, ArrayList, etc. When getting objects from the container, I will cast each result like this:
public void foo(int i) {
try {
Integer result = (Integer) container.get(i);
// do something with result
}
catch(...){..}
try {
Command result = (Command) container.get(i);
// do something with result
}
catch(...){..}
try {
ArrayList<MyClass> result = (ArrayList<MyClass>) container.get(i);
// do something with result
}
catch(...){..}
}
If it stores any type of objects mixed, use Object as storage class. All classes inherit from Object and for primitives use their respective wrapper classes. Or just use one of the bazillion container classes that already exist.
Also instead of using your try statements, think about
instanceof.