Thread: Java Main Method

Results 1 to 6 of 6
  1. #1 Java Main Method 
    Banned
    Join Date
    Jul 2009
    Posts
    327
    Thanks given
    6
    Thanks received
    11
    Rep Power
    0
    Hello all,

    Does there have to be the following method inside EVERY Java program for it to function correctly?:

    Code:
    public static void main(String[] args)
    Or is there some exceptions?
    Reply With Quote  
     

  2. #2  
    Renown Programmer
    Method's Avatar
    Join Date
    Feb 2009
    Posts
    1,455
    Thanks given
    0
    Thanks received
    843
    Rep Power
    3019
    Yes, you need a main method somewhere in your application.
    :-)
    Reply With Quote  
     

  3. #3  
    Programmer, Contributor, RM and Veteran




    Join Date
    Mar 2007
    Posts
    5,074
    Thanks given
    2,625
    Thanks received
    3,579
    Discord
    View profile
    Rep Power
    5000
    It is actually possible to run an app without a main method, for example:

    Code:
    
    public final class NoMainMethod {
    	
    	static {
    		System.out.println("I have no main method, but can still do stuff!");
    		System.exit(0); // required to stop NoSuchMethodError
    	}
    	
    }
    Run like so:

    Code:
    [email protected]:~/test$ java NoMainMethod 
    I have no main method, but can still do stuff!
    [email protected]:~/test$
    This works because when the class is loaded, the contents of the static initializer are executed by the class loader. Then it quits the application to prevent the NoSuchMethodError from being shown. However, I wouldn't recommend doing this!

    If you are developing an applet, your app won't have a main method. Instead, you subclass applet and specify the the applet to load in the <applet> tag. However, ultimately, somewhere along the line there is a main() method (or that nasty trick, unlikely to see that in production code though) which then goes and calls the appropriate applet method.

    There are probably other situations where you won't have a main() method, e.g. if you are writing an app to instrument another app you'll have a premain() method instead. If you are writing a J2EE app, you'll just have a collection of servlets (and some convoluted xml files defining them). Kind of like the applets, another program (the application server) starts it for you.
    .
    Reply With Quote  
     

  4. #4  
    Banned
    Join Date
    Jul 2009
    Posts
    327
    Thanks given
    6
    Thanks received
    11
    Rep Power
    0
    Quote Originally Posted by Graham View Post
    It is actually possible to run an app without a main method, for example:

    Code:
    
    public final class NoMainMethod {
    	
    	static {
    		System.out.println("I have no main method, but can still do stuff!");
    		System.exit(0); // required to stop NoSuchMethodError
    	}
    	
    }
    Run like so:

    Code:
    [email protected]:~/test$ java NoMainMethod 
    I have no main method, but can still do stuff!
    [email protected]:~/test$
    This works because when the class is loaded, the contents of the static initializer are executed by the class loader. Then it quits the application to prevent the NoSuchMethodError from being shown. However, I wouldn't recommend doing this!

    If you are developing an applet, your app won't have a main method. Instead, you subclass applet and specify the the applet to load in the <applet> tag. However, ultimately, somewhere along the line there is a main() method (or that nasty trick, unlikely to see that in production code though) which then goes and calls the appropriate applet method.

    There are probably other situations where you won't have a main() method, e.g. if you are writing an app to instrument another app you'll have a premain() method instead. If you are writing a J2EE app, you'll just have a collection of servlets (and some convoluted xml files defining them). Kind of like the applets, another program (the application server) starts it for you.
    Thanks for sharing your knowledge, Graham. But from what you have just told me i get a strong impression its best to have a main method in Java SE programs, despite being able to do otherwise.
    Reply With Quote  
     

  5. #5  
    Registered Member
    PSNB's Avatar
    Join Date
    Aug 2009
    Posts
    886
    Thanks given
    8
    Thanks received
    103
    Rep Power
    590
    Quote Originally Posted by Zonchord View Post
    Thanks for sharing your knowledge, Graham. But from what you have just told me i get a strong impression its best to have a main method in Java SE programs, despite being able to do otherwise.
    Exactly. Only when developing applications though. When developing applets, you use the four methods that are overridden by the applet, which are
    1. init()
    2. start()
    3. stop()
    4. destroy()
    Reply With Quote  
     

  6. #6  
    Banned
    Join Date
    Jul 2009
    Posts
    327
    Thanks given
    6
    Thanks received
    11
    Rep Power
    0
    Quote Originally Posted by PSNB View Post
    Exactly. Only when developing applications though. When developing applets, you use the four methods that are overridden by the applet, which are
    1. init()
    2. start()
    3. stop()
    4. destroy()
    Thanks PSNB. Whats the difference between an applet and an application? Is an RSPS an applet of application?
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

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