search
HomeJavaJavagetting StartedWhen creating a derived class object, what is the execution order of constructors?

When creating a derived class object, what is the execution order of constructors?

Jul 08, 2020 pm 03:55 PM
objectExecution orderConstructorDerived class

When creating a derived class object, the execution order of the constructor is: base class constructor, derived class object member constructor, and the constructor of the derived class itself. When an object is created, the code loading order is: static code, non-static code, and constructor methods.

When creating a derived class object, what is the execution order of constructors?

When creating a derived class object, the execution order of the constructor is: base class constructor, derived class object member constructor, and the constructor of the derived class itself.

(Recommended tutorial: java introductory program)

Detailed introduction:

The loading order of the code when the object is created is: static code --> Non-static code --> Constructor.

If the parent class is inherited, the loading order is: the static code of the parent class --> the static code of the subclass --> the non-static code within the parent class --> the parent class's Constructor --> Non-static code of the subclass --> Constructor of the subclass.

Among them, static code includes (static methods, static variables, static code blocks, etc.), non-static code is (member methods, member variables, member code blocks, etc.). The same type of code, written above load.

(Video tutorial recommendation: java video tutorial)

Example:

public class ExtendsTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		C c = new D();
	}

}

class C {
	static {
		System.out.println("C 基类静态域 ");
	}
	{
		System.out.println("C 基类对象成员构造函数");
	}

	public C() {
		System.out.println("C 基类本身的构造函数");
	}
}

class D extends C {
	static {
		System.out.println("D 派生类静态域");
	}
	{
		System.out.println("D 派生类对象成员构造函数");
	}

	public D() {
		System.out.println("D 派生类本身的构造函数");
	}
}

Output result:

When creating a derived class object, what is the execution order of constructors?

The above is the detailed content of When creating a derived class object, what is the execution order of constructors?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools