Results 1 to 2 of 2
  1. #1
    Grimmjow is offline Member
    Join Date
    May 2010
    Posts
    12
    Rep Power
    0

    Default Making an object of main and running a thread in main

    Java Code:
      public static void main(String args[]) {
            final Main program = Main.getInstance();
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    try {
                        program.launch();
                    } catch (Exception e) {
                        System.out.println("Failure");
                    }
                }
            });
        }
    1. I've got a question about creating an object of the Main class. Is there any reason for doing this?

    2. And why would you run a thread within main? Is it just too make it look well presented? (the launch code just sets the frame etc. nothing special)

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    I've got a question about creating an object of the Main class. Is there any reason for doing this?
    It depends upon the situation and how it is used in the program. In the example you posted, main implements Runnable that a thread can run.
    And why would you run a thread within main? Is it just too make it look well presented? (the launch code just sets the frame etc. nothing special)
    For many reasons. In the above code you aren't necessarily starting a new thread, you are dispatching the Runnable onto the EDT. SwingUtilities invokelater in main

Similar Threads

  1. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  2. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  3. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:10 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •