16

<init> method can be found in stacktrace, for example. As I understood it represents initialization done in constructor.

If you try to execute

Object.class.getDeclaredMethod("<init>");

You'll get java.lang.NoSuchMethodException.

What is this method? When was it added to class? (in compilation - execution terms) Is it virtual, can one anyhow override it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sergey Fedorov
  • 2,169
  • 1
  • 15
  • 26

2 Answers2

14

Have a look at the Java Virtual Machine Specification, chapter 2.9. It say on the <init> name:

At the level of the Java Virtual Machine, every constructor written in the Java programming language (JLS §8.8) appears as an instance initialization method that has the special name <init>. This name is supplied by a compiler. Because the name is not a valid identifier, it cannot be used directly in a program written in the Java programming language.

That's why <init> can be found on the stack trace, but is not accessible with code.

Kai Sternad
  • 22,214
  • 7
  • 47
  • 42
8

What is an <init> method in Java?

It is a constructor. That's why you get a NoSuchMethodException when you try to call it as a method.

Can it be overriden?

No.


And if you see a <clinit> method, that is the classes static initialization "method".

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