0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

コンストラクタで失敗してもインスタンスはある?

Posted at
public class Main {

    public static Main me;

    public Main() throws Exception {
        this.me = this;
        throw new Exception("constructor error.");
    }

    public void method() {
        System.out.println("It's me");
    }

    public static Main main() {
        return me;
    }

    public static void main(String... args) {
        try {
            Main main = new Main();
        } catch (Exception e) {
            System.out.println("constructor failed.");
        }

        Main main = Main.main();
        main.method();
    }
}

結果

constructor failed.
It's me

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?