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");
    }
}

Recommended Answers

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().

Jump to Post

All 4 Replies

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().

Thanks. How about the first question?

Another question: why can I run init() without any constructor?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.