Object Creation – Explained

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
    • Initializing
      • Static blocks and static variables are initialized
      • Due to lazy loading few of the variables can be initialized only on demand

 

 

 

One thought on “Object Creation – Explained”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Cheat Sheet To JAVA Latest Technology

%d bloggers like this: