Is the constructor "called" against the constructor "executing"?

What is the difference between calling a constructor and executing a constructor? I read about the design dependencies of the order on java-programming langauage from james Gosling. The author claims that when you create an object constructor, it is called first, then the feild members are initialized, finally the costructor is executed. Both sound the same to me.

+4
source share
1 answer

In this context, it is “called” when you call it, and “executed” when the code is actually executed.

Meanwhile, when you call it and the time the code starts, the fields are initialized.

, , , .

:

class Example {

    static int report() { System.out.println("initialize"); return 0; }

    int x = report(); // <- [Step 2] Initialization

    Example () {
        System.out.println("execute"); // <- [Step 3] Execution
    }

}

, :

System.out.println("invoke");
new Example(); // <- [Step 1] Invocation

:

invoke
initialize
execute
+6

Source: https://habr.com/ru/post/1531679/


All Articles