On executing
Car bmw = new Car();Car benz=new Car();Car audi =benz;
References created = 3
Objects created = 2
When Car bmw = new Car() is called the following happens
- Only a reference is created when a variable is defined of a class type. Memory for that variable is allocated only when new() is used.
- JVM allocates enough memory on the heap for the defined instance variables
- Variables are initialized to their default values
- Initialization blocks are executed
- Constructor of the class is called which in turn delegates the call to its super class and the delegation continue until it reaches the Object class
Class Loading
Refer Image : Class Loading
- 3 phases in class loading
- Physical loading
- Required class files are loaded from the system classpath.
- Basic memory structure of the class is calculated at this stage
- Linking
- Verifying
- Bytecode verification through class loader.
- Preparing
- Prepares various data-structures for fields, methods and interface implementation by the class
- Resolving all references
- SuperClasses – Abstract, non-Abstract
- Interfaces
- Variables
- Types defined in method signature
- Local variables defined in methods
- Verifying
- Initializing
- Static blocks and static variables are initialized
- Due to lazy loading few of the variables can be initialized only on demand
- Physical loading
Great insgiht. Relieved I’m on the same side as you.
LikeLike