Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. public class MyApp {
  2.  
  3. private static MyApp instance = null;
  4.  
  5. private static boolean started = false;
  6.  
  7. public MyApp() {
  8. if (instance != null) {
  9. throw new IllegalStateException("MyApp already initialized");
  10. }
  11. instance = this;
  12. }
  13.  
  14. public static MyApp getInstance() {
  15. return instance;
  16. }
  17.  
  18. public void startup() {
  19. synchronized (LOCK) {
  20. if (started == true) {
  21. throw new IllegalStateException("MyApp is already running");
  22. }
  23. }
  24. // do things to start the GUI, etc...
  25. // ... ... ...
  26. started = true;
  27. }
  28.  
  29. }
  30.  
  31. public class Launcher {
  32.  
  33. public static void main(String[] args) {
  34. new Launcher().start();
  35. }
  36.  
  37. private void start() {
  38. // create custom classloader ...
  39. // ... ... ...
  40. Class<?> myAppClass = myLoader.loadClass("com.something.MyApp");
  41. // calls the MyApp constructor and sets the "instance" static var to "this"
  42. Object instance = myAppClass.newInstance();
  43.  
  44. Method startupMethod = myAppClass.getMethod("startup");
  45. // this seems to call the MyApp constructor again!, exception thrown...
  46. startupMethod.invoke(instance);
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement