Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

The applet constructor

A look at the applet constructor's properties and usefulness

  • Print
  • Feedback

QI don't understand what the applet constructor is. Can I override it? Please explain.

ATo write an applet, you must first subclass the Applet class. The Applet class is just like any other class; therefore, an applet constructor is simply the subclass constructor of the Applet class.

Because the applet constructor is just like any other constructor, it cannot be overridden. Constructors perform any necessary initialization for the new object. Remember, you can only override methods, not constructors.

There are five methods in the Applet class that give you the framework on which you build your applet: init(), start(), stop(), destroy(), and paint(). Here, we'll look at the init()) method.

The init() method, which works much like a constructor, handles whatever initialization your applet requires. The browser or applet viewer automatically calls it to perform applet initialization each time the applet is loaded.

In light of this, we recommend that applets do not have constructors because an applet is not guaranteed to have a full environment until its init() method has been called. For example, let's say you want to perform applet image loading inside of an applet constructor. This simply will not work, and should be performed inside the init() method instead.

To sum up, an applet constructor is the constructor of the applet, which has been subclassed from the Applet class. Moreover, this constructor can not be overridden. Therefore, because it is not very useful to define an applet constructor, we advise that you employ an overridden init() method for all initialization.

About the author

Random Walk Computing is the largest Java/CORBA consulting boutique in New York, focusing on solutions for the financial enterprise. Known for their leading-edge Java expertise, Random Walk consultants publish and speak about Java in some of the most respected forums in the world.

Read more about Core Java in JavaWorld's Core Java section.

  • Print
  • Feedback

Resources