I'm not sure about Android (or any restrictions that may have), but in Java you can do something like this:
public static <T> T getObject(String filename) throws IOException, ClassNotFoundException { FileInputStream fis = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(fis); T newObject = (T) in.readObject(); in.close(); return newObject; }
and then name it like
MyClass myObj = getObject("in.txt");
This will give you a warning without warning, though, since the compiler cannot be sure that you can apply the resulting object to the provided type, so it wonβt exactly type safe. You must be sure that what you get from the input stream can indeed be passed to this class, otherwise you will get a ClassCastException. You can suppress the warning by annotating the method with @SuppressWarnings("unchecked")
source share