0

I read it somewhere that calling a constructor is work of JVM, so i created a Class named Hello and did not put anything at all, and just compiled it, after compiling when i open the byte code there is constructor created inside a class which is default constructor. So is it compiler's duty to put a default constructor. I thought it's jvm who checks and calls constructor. Ps: I haven't run that code.

user207421
  • 305,947
  • 44
  • 307
  • 483
Anuj Patel
  • 39
  • 1
  • 5
  • Yes, it is. From [wikipedia](https://en.wikipedia.org/wiki/Default_constructor): "the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors" – Pavel Smirnov Oct 17 '19 at 06:31
  • 4
    I'm not sure where the contradiction is. The JVM calls the constructor, and the compiler creates it in the bytecode. Those are two different things. – Erwin Bolwidt Oct 17 '19 at 06:47
  • They aren't the same thing. You are conflating *calling* a constructor, which happens at runtime, and which therefore has nothing to do with the compiler, with *creating* a constructor, which is done at compile-time, and therefore is done by the compiler. – user207421 Oct 17 '19 at 07:19

3 Answers3

4

I thought it's jvm who checks and calls constructor.

Wrong assumption. The JVM reads compiled classes (.class) files. It doesn't modify or add them.

Of course, the JVM executes code, and thus calls/invokes methods and constructors.

But the java compiler is responsible for "adding" such things a default constructor, see here for more details.

Having said that, of course there is the JIT (just in time compiler) that is part of the JVM. But the JIT translates byte code into machine code, and its job is again, not to add things such as additional constructors.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    _It doesn't modify or add them_ A minor note that JVM HotSpot actualy uses a modified class file structue in comparison to what `javac` put in class-files. See [Rewriter](https://hg.openjdk.java.net/jdk/jdk12/file/06222165c35f/src/hotspot/share/interpreter/rewriter.cpp#l579). Particularly during class initialization `ConstantPool` indexes that were read from the class-file directly in BigEndian order are replaced with `ConstantPoolCache` indexes in native byte order. – St.Antario Oct 17 '19 at 09:56
  • 1
    There seems to be a fundamental language problem. The phrase “checks and calls constructor” does not imply adding a constructor. The OP seems to assume that though, as indicated by looking at the class file for the constructor, however, dismissing this assumption should not applied to the original literal statement. Both, “check” and “call” are too generic terms to assign a truth value to that phrase at all. – Holger Oct 23 '19 at 13:14
0

I am trying to work out what you mean by:

I thought it's jvm who checks and calls constructor.

The "call" makes sense.

The "check" .... not sure. If you mean that the JVM's classloader checks that the required constructors are present when it loads1 the class, that is correct. But if the JVM finds that a (default or otherwise) constructor is missing, it doesn't just add one. Instead the JVM marks the class and its dependents as unusable, throws an Error exception, and typically exits.

(Note that the kind of checks described above are done to deal with cases where there is a binary compatibility mismatch between versions of classes used at compile time and at runtime. Typically the you compiled a class against one version of an API and used and put an incompatible version on the runtime classpath.)


The checking that you are probably thinking about is done by the bytecode compiler.

  • If there is no constructor in the source code of a class, the compiler defines a default constructor, and includes it the .class file. This is in conformance to what the JLS says.

  • If the source code contains a new that uses any constructor that hasn't been defined, the compiler treats this as a compilation error.

By the time the JVM sees any bytecode file for a Java class, it will contain at least one constructor.


1 - I am deliberately leaving out some details here.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-3

A default constructor is automatically generated by the compiler if you do not explicitly define at least one constructor in your class. You've defined two, so your class does not have a default constructor.A default constructor is created if you don't define any constructors in your class. It simply is a no argument constructor which does nothing. Edit: Except call super()

public Module(){
}
Basharat Ali
  • 1,099
  • 9
  • 11
  • 2
    The OP wrote "i created a Class named Hello and did not put anything at all" yet you claim here "you've defined two, so your class does not have a default constructor". That directly contradicts what the OP wrote. Do you know the OP and did you get information that we don't see? – Erwin Bolwidt Oct 17 '19 at 10:29