Who provides the default constructor in Java? Compiler or JVM?

Is the constructor added at runtime or compile time? (I think this is compilation time). I need a detailed explanation, please, at the JVM architecture level.

I read various articles. Some say the compiler ... and others say the JVM. I want to be very sure (evidence will help a lot).

Sorry if the question is stupid (I'm still digesting terminology) !!!

Thanks in advance.

+4
source share
2 answers

From a Java tutorial from Oracle: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

- , . . . , , , . , Object, .

+6

:

, Oracle JDK OpenJDK, , , , .

, , Java, JDK, , - .

$JDK_HOME/bin/

javap

Demo.java, ,

public class Demo {}

javac Demo.java, javap -c Demo, - :

Compiled from "Demo.java"
public class Demo {
  public Demo();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return
}

, , , .

, , . ,

public class Demo { 
  protected static class Other {}
}

, javap -c Demo.Other,

public class Demo$Other {
  protected Demo$Other();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return
}

, , , , , , .

+5

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


All Articles