Order of execution of instance variables

Senthil Kumaran

I know about static and intialization block in a class and their order of execution. But the following code causes some doubts.

class Test1 {
   Test1(int x) {
      System.out.println("Constructor value : " + x);
   }
}

public class Test2 {
   Test1 t1 = new Test1(10);    ----- line 8
   Test2(int i) {
       t1 = new Test1(i);
   } 

  public static void main(String[] args) {
     Test2 t2 = new Test2(5);
  }
}

This code's output :
Constructor value : 10
Constructor value : 5

My question is that the instance variable (line 8) is executed as soon as the object for the class(Test2) is created or some other stuff happens.

T.J. Crowder

Initializers and initialization blocks are executed as though they were inserted into every constructor right at the top (in a base class) or just after the call to super(...) (in a subclass).¹ So your Test2 is functionally identical to:

public class Test2 {
   Test1 t1;
   Test2(int i) {
       t1 = new Test1(10);
       t1 = new Test1(i);
   } 

  public static void main(String[] args) {
     Test2 t2 = new Test2(5);
  }
}

¹ In fact, that's literally true if you look at the bytecode...

For instance, suppose we add a second constructor to Test2:

public class Test2 {
    Test1 t1 = new Test1(10);

    Test2(int i) {
        t1 = new Test1(i);
    } 

    Test2(int i, String s) {
        t1 = new Test1(i);
    } 

    public static void main(String[] args) {
        Test2 t2 = new Test2(5);
    }
}

then compile it, and use javap -c Test2 to see the bytecode (roughly):

Compiled from "Test2.java"
public class Test2 {
  Test1 t1;

  Test2(int);
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: new           #2                  // class Test1
       8: dup
       9: bipush        10
      11: invokespecial #3                  // Method Test1."<init>":(I)V
      14: putfield      #4                  // Field t1:LTest1;
      17: aload_0
      18: new           #2                  // class Test1
      21: dup
      22: iload_1
      23: invokespecial #3                  // Method Test1."<init>":(I)V
      26: putfield      #4                  // Field t1:LTest1;
      29: return

  Test2(int, java.lang.String);
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: new           #2                  // class Test1
       8: dup
       9: bipush        10
      11: invokespecial #3                  // Method Test1."<init>":(I)V
      14: putfield      #4                  // Field t1:LTest1;
      17: aload_0
      18: new           #2                  // class Test1
      21: dup
      22: iload_1
      23: invokespecial #3                  // Method Test1."<init>":(I)V
      26: putfield      #4                  // Field t1:LTest1;
      29: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #5                  // class Test2
       3: dup
       4: iconst_5
       5: invokespecial #6                  // Method "<init>":(I)V
       8: astore_1
       9: return
}

You can see the t1 = new Test1(10) in both constructors.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Order of execution of instance variables

From Dev

Perl variables order of execution

From Dev

Get instance variables in order in Python

From Dev

Get instance variables in order in Python

From Dev

Order of execution of static blocks and instance variable

From Dev

What are the values of these Ruby instance variables throughout execution?

From Dev

Order of type-variables in instance-declarations

From Dev

How the order of execution is performed between static variables and blocks?

From Dev

C++: Control order of thread execution with mutex's and conditional variables

From Dev

C++: Control order of thread execution with mutex's and conditional variables

From Dev

How to force java object to instantiate it's instance variables in specific order?

From Dev

constructor execution sequence/order: dependent initialization of static variable (class instance) in a function

From Dev

C# - Why does execution flow to instance variables when a class constant is referenced?

From Dev

C# - Why does execution flow to instance variables when a class constant is referenced?

From Dev

events execution order

From Dev

Issue in execution order with RewriteRules

From Dev

Order of ON and JOIN in query execution

From Java

Decorator execution order

From Dev

Order of threads in execution

From Dev

CUDA thread execution order

From Dev

ListenableFuture callback execution order

From Dev

Order of execution in a Makefile

From Dev

Controlling order of execution in angularjs

From Dev

Execution order of Enum in java

From Dev

Execution order of ExceptionMapper

From Dev

`var(A)` and order of execution

From Dev

Order promises execution in AngularJs

From Dev

Maven plugins execution order

From Dev

Execution order of JavaScript listener

Related Related

  1. 1

    Order of execution of instance variables

  2. 2

    Perl variables order of execution

  3. 3

    Get instance variables in order in Python

  4. 4

    Get instance variables in order in Python

  5. 5

    Order of execution of static blocks and instance variable

  6. 6

    What are the values of these Ruby instance variables throughout execution?

  7. 7

    Order of type-variables in instance-declarations

  8. 8

    How the order of execution is performed between static variables and blocks?

  9. 9

    C++: Control order of thread execution with mutex's and conditional variables

  10. 10

    C++: Control order of thread execution with mutex's and conditional variables

  11. 11

    How to force java object to instantiate it's instance variables in specific order?

  12. 12

    constructor execution sequence/order: dependent initialization of static variable (class instance) in a function

  13. 13

    C# - Why does execution flow to instance variables when a class constant is referenced?

  14. 14

    C# - Why does execution flow to instance variables when a class constant is referenced?

  15. 15

    events execution order

  16. 16

    Issue in execution order with RewriteRules

  17. 17

    Order of ON and JOIN in query execution

  18. 18

    Decorator execution order

  19. 19

    Order of threads in execution

  20. 20

    CUDA thread execution order

  21. 21

    ListenableFuture callback execution order

  22. 22

    Order of execution in a Makefile

  23. 23

    Controlling order of execution in angularjs

  24. 24

    Execution order of Enum in java

  25. 25

    Execution order of ExceptionMapper

  26. 26

    `var(A)` and order of execution

  27. 27

    Order promises execution in AngularJs

  28. 28

    Maven plugins execution order

  29. 29

    Execution order of JavaScript listener

HotTag

Archive