Link to home
Start Free TrialLog in
Avatar of farooqnasim
farooqnasim

asked on

about applets

in an applet if constructor, init() method, start() method and paint() method are called, what will be the execution flow of these methods
ASKER CERTIFIED SOLUTION
Avatar of superschlonz
superschlonz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of farooqnasim
farooqnasim

ASKER

i knew all these things, there is nothing new, i repeat the question again,
      see the code below

import java.applet.Applet;
import java.awt.*;
class MyApplet extends Applet{
          Button b1,b2;
          MyApplet(){
          b1=new Button("one");
           add(b1);
           setBackground(Color.blue);
       }            
          public void init(){
          b2=new Button("Two");
          add(b2);

          setBackground(Color.red);
       }
}
now whether init() or constructor will be called first
or both at the same time and if a new thread is created in constructor, then what will happen
There must be an instance of MyApplet to call init.
So first the constructor must be called. After that init will be called.

I compiled your code and the applet displays 2 buttons and the background is red.

But I had to change 2 lines for getting it running: The class must be public and the
constructor also.

Now it looks like this:

...
public class MyApplet extends Applet {
  ...
  public MyApplet() {
  ...
}
Oh I forgot the Thread.

If you create a thread in constructor and start it, it will run and do its work. But you
should start threads in init() if they initialize something or in start() if they show an
animation for example.
I don't know what will happen if you start it in the constructor and the user presses
reload, perhaps only stop(), destroy(), init() and start() are called.
the answer is again incomplete, if a thread is created inside the constructor of an applet then what will happen, what method will be executed first, init() or start()
They are still called in the same order:
Constructor, init, start