Dave's Brain

Browse - Programming Tips - How can load (make an instance) of a class that is compiled in with my program?

Date: 2013oct21
Language: Java

Q.  How can load (make an instance) of a class that is compiled in with my program?

A.  Of course you can do:

Class<?> cls = null; if (something1) { cls = new MyClass1; } else if (something2) { cls = new MyClass2; }
But if you want to use reflection:
Class<?> loadMyClass(String name) { final String classname = "com.mycompany.myprogram" + name; try { return Class.forName(classname); } catch (Exception e) { return null; } }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.