• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

why void main() in swing and not in applet

 
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().


 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
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.
 
Rancher
Posts: 43016
76
  • 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).
 
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. ]
    Bookmark Topic Watch Topic
  • New Topic