Why are non-static methods allowed in applets?

263 Views Asked by At

I've noted that adding a non-static method to the class containing the main method in a java-application and then calling it from within the main-method, results in a compilation error. This I can understand, since this class is never instantiated.

However, adding a non-static method to an Applet-class and calling it from within, say, the paint method works fine. Why is this? Is the applet-class somehow instantiated by appletviewer, or is there another explanation as to why the former isn't allowed while the latter is?

1

There are 1 best solutions below

0
On BEST ANSWER

Is the applet-class somehow instantiated by appletviewer

Absolutely. See the "Life Cycle of an Applet" section of the Applets tutorial. In particular:

As a result of the applet being loaded, you should see the text "initializing... starting...". When an applet is loaded, here's what happens:

  • An instance of the applet's controlling class (an Applet subclass) is created.
  • The applet initializes itself.
  • The applet starts running.