JApplet Lifecycle: init(), start(), stop(), destroy() – Full Execution Flow
JApplet Lifecycle: init(), start(), stop(), destroy() – Full Execution Flow
JApplet क्या है?
JApplet Java Swing का एक GUI-based applet class है जो web browser या Applet Viewer में चलाया जा सकता है। यह javax.swing.JApplet class को extend करता है और इसका इस्तेमाल छोटे interactive programs या graphical user interfaces बनाने में किया जाता है।
JApplet, पुराने AWT (Abstract Window Toolkit) Applet का upgraded version है। इसका फायदा यह है कि इसमें आप Swing components जैसे – JButton, JLabel, JTextField आदि को use कर सकते हैं, जो अधिक flexible और user-friendly होते हैं।
हर JApplet का एक lifecycle होता है — यानी यह कुछ stages से होकर गुजरता है जैसे कि initialization, starting, stopping, और destruction। अब हम step-by-step इन चारों stages को समझेंगे।
JApplet Lifecycle Methods
JApplet में चार main lifecycle methods होते हैं जो applet के execution flow को control करते हैं:
- init() – Applet को initialize करने के लिए
- start() – Applet को start या resume करने के लिए
- stop() – Applet को temporarily stop करने के लिए
- destroy() – Applet को permanently remove करने के लिए
Browser या Applet Viewer इन methods को automatically call करता है जब Applet load, run, stop या close होता है।
1. init() Method
init() method Applet के load होते ही सबसे पहले call होता है। इस method का काम Applet को initialize करना होता है, जैसे कि variables set करना, components बनाना और layout design करना।
यह method एक बार ही execute होता है, जब Applet पहली बार load किया जाता है।
public void init() {
setBackground(Color.lightGray);
setLayout(new FlowLayout());
JLabel label = new JLabel("Welcome to JApplet Example!");
add(label);
}
ऊपर दिए गए code में init() method Applet के background color को set करता है और एक JLabel component को add करता है।
2. start() Method
start() method हर बार तब call होता है जब Applet user के browser में active होता है या page reload होता है।
अगर user Applet से किसी दूसरे tab या page पर चला जाए और फिर वापस आए, तो start() method दोबारा run होता है।
public void start() {
System.out.println("Applet Started...");
}
यह method background में कोई animation, timer या task start करने के लिए use किया जाता है।
3. stop() Method
stop() method तब call होता है जब user Applet को छोड़कर किसी दूसरे page या tab पर चला जाता है। इसका use background tasks या animation को temporarily रोकने के लिए किया जाता है।
public void stop() {
System.out.println("Applet Stopped.");
}
इससे resources save रहते हैं और unnecessary processing नहीं होती।
4. destroy() Method
destroy() method Applet को permanently remove करने से पहले call होता है। इसका काम cleanup करना होता है, जैसे memory release करना या open connections को close करना।
public void destroy() {
System.out.println("Applet Destroyed.");
}
यह method केवल एक बार call होता है, जब Applet पूरी तरह से unload किया जाता है।
JApplet का Full Execution Flow
JApplet के lifecycle का पूरा flow नीचे दिए गए diagram और table के माध्यम से समझा जा सकता है।
| Method | Execution Time | Purpose |
|---|---|---|
| init() | Applet load होने पर | Initialization और component setup |
| start() | Applet display या activate होने पर | Tasks या animations start करना |
| stop() | Applet inactive या tab change होने पर | Background process को stop करना |
| destroy() | Applet unload या browser close होने पर | Cleanup और resources release करना |
Execution Order
इन methods का execution order हमेशा same रहता है:
init() → start() → stop() → destroy()
अगर user Applet को reload करता है, तो start() और stop() बार-बार execute होते हैं लेकिन init() और destroy() केवल एक बार चलते हैं।
JApplet Lifecycle Example Program
अब नीचे एक complete example दिया गया है जिससे आप समझ सकते हैं कि ये चारों methods practically कैसे काम करते हैं।
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class LifeCycleExample extends JApplet {
JLabel label;
public void init() {
label = new JLabel("Applet Initialized");
add(label);
System.out.println("init() called");
}
public void start() {
label.setText("Applet Started");
System.out.println("start() called");
}
public void stop() {
label.setText("Applet Stopped");
System.out.println("stop() called");
}
public void destroy() {
System.out.println("destroy() called");
}
}
इस program को run करने पर browser या Applet Viewer के console में messages दिखेंगे जिससे आपको lifecycle का पूरा flow clear समझ आएगा।
JApplet Lifecycle Visualization
JApplet का lifecycle एक process की तरह चलता है। इसे आप इस तरीके से visualize कर सकते हैं:
- init() → Applet load हुआ और initialize किया गया
- start() → Applet active हुआ और display हुआ
- stop() → Applet inactive हुआ (जैसे tab change)
- destroy() → Applet unload हुआ या close हुआ
इस sequence को याद रखना बहुत जरूरी है क्योंकि यह college exams में frequently पूछा जाता है।
init() और start() में Difference
यह question बहुत बार viva और exams में पूछा जाता है, इसलिए इसे ध्यान से समझो:
| Basis | init() | start() |
|---|---|---|
| Call Timing | Applet load होने पर | Applet display होने पर |
| Execution Count | एक बार | हर बार जब Applet active होता है |
| Purpose | Initialize करना | Execution start करना |
| Usage Example | Components बनाना | Animation या process शुरू करना |
Important Points to Remember
- JApplet Swing का subclass है जो Applet को modern GUI के साथ extend करता है।
- init() और destroy() केवल एक बार run होते हैं।
- start() और stop() बार-बार execute हो सकते हैं।
- Lifecycle methods को manually call नहीं किया जाता — इन्हें Applet Viewer या browser call करता है।
- JApplet में आप Swing components का use कर सकते हैं जबकि Applet में केवल AWT components का।
- Applet Viewer tool का use testing के लिए किया जाता है।
Exam-Focused Notes
- init(), start(), stop(), destroy() – ये चार lifecycle methods Applet के execution को control करते हैं।
- init() initialization के लिए use होता है और यह एक बार चलता है।
- start() हर बार तब चलता है जब Applet active होता है।
- stop() तब call होता है जब user Applet से बाहर जाता है।
- destroy() Applet बंद होने से पहले cleanup के लिए चलता है।
- Execution order हमेशा init() → start() → stop() → destroy() रहता है।
- Exam में difference between init() and start() या full lifecycle flow diagram अक्सर पूछा जाता है।
- JApplet class javax.swing package में होती है।