Java Applet Life Cycle: Initialization, Execution and Termination

Learn the legacy applet callback model per instance, then trace exact field values across repeated start, paint, stop and destroy events.

KnowledgeGate Team

Exam prep & CS education

Updated 22 Nov 20266 min read
Java Applet Life Cycle: Initialization, Execution and Termination

The mnemonic init() -> start() -> paint() -> stop() -> destroy() hides that start() and stop() can repeat for a retained instance, while paint() can run any number of times. For one instance, score changes 10 -> 13 -> 11 -> 14 -> 12 -> 0, start() runs twice, and paint() runs three times. Applets are legacy Java lifecycle material, not browser technology to deploy. Revise the broader class and inheritance context in Object Oriented Technology Explained: Four Pillars in Java.

Build the per-instance life cycle before memorising methods

A legacy browser or applet viewer creates an Applet subclass instance and controls its callbacks; there is no main(). For one instance, init() precedes the first start(). An active phase may receive any number of paint requests. stop() ends it, and a later start() may reopen it if the host retains the instance. Reclamation follows stop() with destroy().

The useful invariant is:

new instance -> init once -> [start -> paint zero or more times -> stop] one or more active cycles -> destroy once

The bracketed cycle repeats only when the stated host events retain the instance.

The host calls init(), start(), stop() and destroy(). AWT invokes paint(Graphics) when needed. Code may request rendering with repaint(), but paint() is not a mandatory fifth milestone.

State diagram of one legacy applet instance showing init at score 10, start phases reaching scores 13 and 14, paint counts 1 to 3, and destruction returning score to 0.

Give each callback one clear responsibility

The Java SE 8 Applet API guarantees init() before the first start(), and stop() before destroy().

Method

Typical job

Frequency for one instance

Do not confuse it with

init()

One-time setup after the host environment exists

Once

The constructor

start()

Start or resume active work; return quickly

One or more times

Initialization

paint(Graphics)

Draw current state when AWT requests it

Zero or many times

A cycle counter

stop()

Pause work; release activity-only resources

Per ended active phase

Final deletion

destroy()

Final cleanup before reclamation

Once

Lengthy essential work

Oracle warns that a long destroy() may not finish, so essential state should not depend on lengthy shutdown. Constructors create objects; applet-environment work belongs in init(). As an AWT callback, paint() cannot measure starts, visits or active cycles.

Work the counter applet through two active cycles

This is historical Java SE 8 lifecycle code, not current-browser or current-JDK code. paints changes only to expose callback count.

java
import java.applet.Applet;
import java.awt.Graphics;

public class ScoreApplet extends Applet {
    private int score;
    private int starts;
    private int paints;
    private boolean active;

    public void init() {
        score = 10;
        starts = 0;
        paints = 0;
        active = false;
    }

    public void start() {
        starts++;
        score += 3;
        active = true;
    }

    public void paint(Graphics g) {
        paints++;
        g.drawString(
            "score=" + score + ", starts=" + starts
                + ", paints=" + paints + ", active=" + active,
            10, 20
        );
    }

    public void stop() {
        score -= 2;
        active = false;
    }

    public void destroy() {
        score = 0;
        active = false;
    }
}

Apply only the listed events, in order:

Event

score

starts

paints

active

visible text if painted

init

10

0

0

false

None

start #1

13

1

0

true

None

paint #1

13

1

1

true

score=13, starts=1, paints=1, active=true

paint #2 after an expose event

13

1

2

true

score=13, starts=1, paints=2, active=true

stop #1

11

1

2

false

None

start #2

14

2

2

true

None

paint #3

14

2

3

true

score=14, starts=2, paints=3, active=true

stop #2

12

2

3

false

None

destroy

0

2

3

false

None

Score is 10 + 3 = 13, 13 - 2 = 11, 11 + 3 = 14, 14 - 2 = 12; destroy() assigns 0. Only painting changes paints.

Event strip showing nine applet lifecycle stages from init [10,0,0,F] through two starts, three paints, two stops, and destroy [0,2,3,F].

Understand why paint() breaks the five-method mnemonic

AWT may request paint(Graphics) for first display, exposure, resizing or repaint. Two paints between one start() and stop() still belong to one active cycle. The safer model has four lifecycle milestones plus AWT rendering.

Trace A is init, start, paint, paint, stop. Its counts are init=1, start=1, paint=2, stop=1, destroy=0. Trace B is init, start, stop, start, paint, stop, destroy. Its counts are init=1, start=2, paint=1, stop=2, destroy=1. Both are legal because their stated host schedules contain different numbers of active phases and paint requests.

repaint() requests future painting. It does not synchronously prove that paint() has completed. Normal application design keeps data updates outside rendering; this example increments paints only as instrumentation. For related revision on mutable and immutable state, read String Handling in Java: Pool and Immutability. Applet fields are not automatically persistent.

Distinguish pause, return, reload and final termination

Host scenario

Callback consequence

Page inactive, instance retained

stop, then later start

Instance reclaimed

stop before destroy

Reload creates a fresh instance

Old stop -> destroy; new init -> start

Redraw while active

Another paint; no new init or start

If the instance is retained after stop #1, its second start adds 3 to score=11 and reaches 14. If it is destroyed and replaced, the new init() resets score to 10, then its first start() reaches 13. This distinguishes resumption from construction.

The Java SE 8 API supplies the method rules. Oracle's Java 8 applet lifecycle tutorial illustrates reload and new-instance behaviour, but does not establish universal browser caching. OpenJDK JEP 504, closed and delivered for JDK 26, records the Applet API's removal after browser and JDK execution support had disappeared.

Repair the common lifecycle mistakes

  • Treating init() as a constructor → it is a host callback after construction.

  • Expecting main() → applet execution is host-controlled.

  • Assuming start() runs once → it may run again for a retained instance.

  • Assuming one paint() follows every start → painting may run zero or many times.

  • Using stop() as final deletion → it pauses activity and may be followed by start().

  • Doing all cleanup only in destroy() → suspend active work in stop() and keep final cleanup short.

Order drill: init, start, paint, stop, start, paint, stop, destroy is valid for a retained instance. start before init breaks initialization order. destroy before the final stop breaks termination order. A second init breaks the once-per-instance rule. Two paints cannot force two starts because painting is zero-or-more work inside an active phase.

Java Applet Life Cycle practice on KnowledgeGate reinforces callback order and state tracing. Practice availability does not establish official recurrence or topic weightage.

How exam-style questions test applet life-cycle reasoning

Use these practice formats: arrange callbacks; match their roles; count calls from a schedule; distinguish retained and fresh instances; predict fields across cycles; and explain repeated paints.

Use this 20-second routine: mark the instance boundary; put init before the first start; bracket each start ... stop; insert every stated paint but invent none; place stop before destroy; update fields once per event. The result is 10 -> 13 -> 13 -> 13 -> 11 -> 14 -> 14 -> 12 -> 0, with init=1, start=2, paint=3, stop=2, destroy=1.

Fresh check: init() sets value=4, each start() doubles it, each stop() subtracts 3, and destroy() sets 0. For init, start, stop, start, stop, destroy:

  1. init: value = 4.

  2. First start: 4 x 2 = 8.

  3. First stop: 8 - 3 = 5.

  4. Second start: 5 x 2 = 10.

  5. Second stop: 10 - 3 = 7.

  6. destroy: assign value = 0 before reclamation.

Short version and the next Java study step

  • One init() runs per instance.

  • start() begins or resumes active work.

  • paint() renders whenever AWT asks.

  • stop() pauses and always precedes destroy().

  • destroy() performs final cleanup.

  • A fresh instance restarts at init(), while a retained instance resumes with its fields.

Under the explicit nine-event schedule, one instance reaches score=14 on its second start, receives three paints across two active cycles, then moves through 12 at the final stop to 0 at destroy.

Use Java Programming for the wider language sequence, DSA using Java for implementation practice, or Coding & DSA Courses for Placements for broader navigation. None advises applet deployment.