Ullas
Ullas

Reputation: 837

Steps involved in instantiation of Class?

When we write a statement like Foo f = new Foo(); we know that JVM calls the Defaut ClassLoader.loadClass(), which return instance of Class , now how do we get our Foo instance from Class ?

Upvotes: 2

Views: 473

Answers (1)

Tom Anderson
Tom Anderson

Reputation: 47173

I'm not entirely sure what you're asking, but if you're asking what code is executed when you create a new instance of a class, then the answer is that this is a primitive operation of the Java virtual machine, and there isn't any Java code involved.

The behaviour of instance creation is specified by the section 12.5. Creation of New Class Instances of the Java Language Specification.

There is also a section on 4.10.2.4. Instance Initialization Methods and Newly Created Objects in the Java Virtual Machine Specification, but that's not all that interesting.

To find out what actually happens when you create an object, you would need to choose a JVM implementation and read its source code. Alternatively, you might do what most Java programmers do, and think of it as an essentially magical operation that just works!

Upvotes: 4

Related Questions