By running following code and I got following output:

At begining of default constructor, theApplet.isDisplayable()=false
Start Init here...
At begining of init(), theApplet.isDisplayable()=true
At begining of constructor, theApplet.isDisplayable()=false
End Init...

Two questions:
1. Does anybody know why isDisplayable() is changed without do anything on it?
2. Why does the JApplet not run init first, but default constructor?

import javax.swing.*;


public class TestJApplet extends JApplet{

    public  void init() {

          System.out.println("Start Init here...");
          /*this.setVisible(true);
          this.validate();*/



          if (this.isDisplayable()) System.out.println("At begining of init(), theApplet.isDisplayable()=true");
            else System.out.println("At begining of init(), theApplet.isDisplayable()=false");

          new TestJApplet("test");

          System.out.println("End Init...");
      }

    public TestJApplet(){

        if (this.isDisplayable()) System.out.println("At begining of default constructor, theApplet.isDisplayable()=true");
        else System.out.println("At begining of default constructor, theApplet.isDisplayable()=false");
    }

    public TestJApplet(String test){


        if (this.isDisplayable()) System.out.println("At begining of constructor, theApplet.isDisplayable()=true");
        else System.out.println("At begining of constructor, theApplet.isDisplayable()=false");
    }
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Why does the JApplet not run init first, but default constructor?

init() is an instance method. It cannot be called without an instance. You can't get an instance without executing a constructor. So there will always be a constructor executed before init().

If you don't supply a constructor then the compiler gives you a default no-args constructor that just calls the superclass no-args constructor. SO there is always a constructor and it's always run before any instance method can be called.

This article has been dead for over six months. Start a new discussion instead.