• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why void main() in swing and not in applet

 
sakshi saw
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi could you clear my doubt regarding using init() in case of awt and main in case of swing??

and what should come in start() and init().


 
Rob Spoor
Sheriff
Pie
Posts: 20753
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the way applets are created.

Swing applications (and all other non-applets) are called by the JVM directly. It loads the class specified on the command line (e.g. "java test.Main" loads class "test.Main"), finds its "public static void main(String[])" method and calls that.

Applets are called indirectly by the browser (or applet viewer). The browser reads the HTML file in which the applet is placed, finds its class name and instantiates it. It then calls the init() method to finish the loading. It then calls start() every time the applet is shown again (by loading the HTML page), and stop() when the applet is no longer visible (by loading a different HTML page).

The init() method should contain code that is necessary for the applet to run. Usually this is loading parameters etc. I'm not sure if GUI creation should go in this method, or in the applet constructor. I doubt that matters. I think it's the code that reads external information from the HTML page (getAppletInfo, getCodeBase, getParameter etc) that cannot go in the constructor.

The start() and stop() methods are not used as much as the init() method. They could be used to start and stop a little movie, or an audio clip, or a thread, or something like that.


Moving to Applets.
 
Steve Jefferson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its the way applets are to be executed and it involves in network transport. So it has to be initialized and run rather than executing as stand alone applications.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has nothing to do with "network transport"; it's simply the way the applet lifecycle has been defined. It would have been perfectly possible to base the applet lifecycle on a "main" method instead.

As to doing initialization in init() vs. the constructor: it's good practice never to use constructors in applets (just like in servlets).
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ UD: Content removed. Please don't hijack existing topics by posting unrelated questions; start a new topic instead. ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic