Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

java - Why do applets not need a main()?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...