[Solved] default constructor is it called or not in this example [closed]


If you decompile your class using javap, you’ll find:

Compiled from "A.java"
class A {
  int a;

  int b;

  int c;

  A();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: iconst_3
       6: putfield      #2                  // Field a:I
       9: aload_0
      10: iconst_4
      11: putfield      #3                  // Field b:I
      14: return
}

Even without knowing what this bytecode means, you can see that a and b are mentioned in the constructor (this is where they are assigned in the constructor), but the only mention of c is in the field declaration, int c. So nothing happens to c in the constructor.

Because it is not explicitly initialized, it will have the default initial value, as described in JLS Sec 4.12.5.

Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):

  • For type int, the default value is zero, that is, 0.

So c will have the value zero, but not as a result of the invocation of the constructor (which is distinct from the creation of the instance, as far as the JVM is concerned).

3

solved default constructor is it called or not in this example [closed]