0

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 by Nick Evan: Fixed formatting

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

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

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.