1
 class p {
     p(){
         System.out.println("1");
     }
 }
class Student extends p{  
    Student() {
        super();
        System.out.println("2");
    }

    public static void main(String[] args) {
        Student s =  new Student();
    }
}

As I have provided super() explicitly, but after de-compiling using jd-gui I am not able see super() where I have placed, as super() should be provided by compiler itself. I just want to see where this, super keyword and other things are provided by compiler implicitly. I googled and found that .class file I can see with jd-gui but I am not able to see super() and this() in that, is there any other way for the same? image

venkatvb
  • 681
  • 1
  • 9
  • 24
saurabh kumar
  • 155
  • 5
  • 26
  • 1
    The JVM will automatically try to call the non-argument constructor of the extended class as the first command in the sub class constructor. So this part was removed in the code optimization. A command like `super(someArgument)` should stay in your code. – Tom Jul 13 '14 at 12:08
  • @BlithE so there is no way to see the default constructor super() ? – saurabh kumar Jul 13 '14 at 12:11
  • No, the JVM will call it anyway. But it is necessary to call super explicitly if you provide constructor arguments. – Tom Jul 13 '14 at 12:19
  • The JVM has nothing to do with this. The Java compiler (e.g., javac) inserts an implicit `super()` call when one is missing; the call is in the bytecode, whether it was explicitly written in the original Java source or not. See Ian's answer. – Mike Strobel Jul 13 '14 at 21:16

2 Answers2

7

In Java, any constructor that does not start with an explicit super(...) or this(...) chaining constructor call will automatically get (the bytecode equivalent of) super(); inserted as its first instruction by the compiler. Therefore when the decompiler sees the bytecode for super() at the start of a constructor it has no way of knowing whether the original source code included an explicit super() or whether the instruction was inserted implicitly by the compiler. Both versions compile to the same bytecode so there's no particular reason for it to prefer one over the other.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1

Implicit constructor super() is called anyway in your decompiled code.

Class A extends B {
    public A() {
        super(); // this is called whether you put it here or not
    }
}
stuhlo
  • 1,479
  • 9
  • 17