Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do applets not need a main()?

Tags:

java

main

applet

This applies to subclasses of Applet, Servlet, Midlet, etc.

Why do they not need a main()? If I wanted to create a Craplet class that starts at init() or something similar, is it bad design, or how would I go about doing it?

like image 319
Lucky Avatar asked May 31 '09 13:05

Lucky


People also ask

Do applets require a main method?

Unlike Java programs, applets do not begin execution at main( ). In fact, most applets don't even have a main( ) method. Instead, an applet begins execution when the name of its class is passed to an applet viewer or to a network browser.

What are the limitations of applets?

Applets cannot stream data directly into the browser. Applets cannot subscribe to events detected by the browser that are triggered outside of the applet area. For example, applets cannot detect that a user followed a bookmark. Similarly, applets cannot detect that a user typed in a URL that should be followed.

Why are applets needed?

Java applets help make websites more interactive and dynamic. Many pieces must fit together to make an applet work. A Java applet is a small piece of code that works within another program. A Sandbox is a testing environment.

Why applet is not used in Java?

Support for running Applets in browsers was only possible while browser vendors were committed to standards-based plugins. With that no longer being the case, Applet support ended in March 2019. Oracle announced in January 2016 that Applets would be deprecated in Java SE 9, and the technology was removed in Java SE 11.


1 Answers

It is actually good design but not obvious and what you want to do would have no effect so it is a little counter intuitive.

These types of applications live their lives in containers and as such their entry points are determined by the standards those containers must adhere to. The designers of these standards chose not to call the entry point main. You would place your functionality in an overridden method. All applets have the following four methods:

public void init();
public void start();
public void stop();
public void destroy();

They have these methods because their superclass, java.applet.Applet, has these methods.

The superclass does not have anything but dummy code in these:

public void init() {}

If you want to derive a class to extend or change the name of init() you should Implement your class and have your method call init(). This would use polymorphism to let you call the method whatever you like. Unless you are writing servlet container you are likely wasting your time.

like image 159
ojblass Avatar answered Oct 09 '22 16:10

ojblass