DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Revisiting Basics: Execution Sequence while loading a class

Upendra Chintala user avatar by
Upendra Chintala
·
Aug. 21, 11 · Interview
Like (0)
Save
Tweet
Share
4.57K Views

Join the DZone community and get the full member experience.

Join For Free

Here, we discuss how Java's call sequence behaves when an object is being instantiated. I explain this with a parent-child class relationship to help you better understand when a class is in hierarchy.

Call Sequence with an example

Our example implements two classes

  1. ParentClass
  2. ChildClass

As the names resemble ChildClass is extending from ParentClass, and we create an instance of ChildClass to monitor the execution call sequence

  1. The first thing that is executed while loading(the object is not yet fully created) a class is static variable initialization and the static blocks in parent class followed by child class static variable initialization and the blocks.
  2. The next thing is the parent class initialization block followed by the parent class constructor.
  3. The last thing is the child class initialization blocks followed by child class constructor.

When the execution control finishes executing sub class constructor, that is when we say the object is fully created and is ready to use.

The following code-snippet and it’s output explains this behavior.

package com.accordess.blogs;
 
/**
 *
 * @author
 */
 
public class ChildClass extends ParentClass{
 
    static{
        System.out.println("Child class static instance block") ;
    }
    {
        System.out.println("Child class instance block") ;
    }
 
    public ChildClass(){
        System.out.println ("Child class constructor") ;
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new ChildClass () ;
    }
}
 
class ParentClass{
    private static int value = 10 ;
    static{
        System.out.println ("Parent Class static innitialization block : "+value) ;
    }
    {
        System.out.println ("Parent Class innitialization block") ;
    }
    protected ParentClass (){
        System.out.println ("Parent Class consutructor") ;
    }
 
}
 
Output:
Parent Class static innitialization block : 10
Child class static instance block
Parent Class innitialization block
Parent Class consutructor
Child class instance block
Child class constructor

From http://accordess.com/wpblog/2011/06/06/revisiting-basics-execution-sequence-while-loading-a-class/

Execution (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing the Right Framework for Your Project
  • Using GPT-3 in Our Applications
  • 3 Main Pillars in ReactJS
  • Introduction to Spring Cloud Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: