search
HomeJavajavaTutorialWhat should you pay attention to when using java construction code blocks?

1. The concept

appears outside the method in the class. It will be executed every time the constructor is called, and it will be executed before the constructor.

2. Usage Notes

(1) The function of the construction code block is to initialize the object.

(2) The construction code block is run as soon as the object is created, and takes precedence over the constructor function

(3) The difference between the construction code block and the constructor function is: the construction code block is for all objects Unified initialization is performed, and the constructor initializes the corresponding object, because there can be multiple constructors. Whichever constructor is run will create what kind of object, but no matter which object is created, the same construction code block will be executed first. In other words, what is defined in the construction code block is the common initialization content of different objects.

3. Execution sequence

When creating an object, the construction code block will be executed first, and then the constructor function will be executed.

4. Example

package com.initialization;
 
/**
 * 构造代码块的实际使用
 */
public class ConstructBlock {
    public static void main(String[] args) {
        System.out.println("****创建第一个学生****");
        Student stu1=new Student("小明");
        System.out.println();
        System.out.println("****创建第二个学生****");
        Student stu2=new Student(13);
    }
}
 
class Student{
    String area;
    String name;
    int age;
    {
        area="北京";
        System.out.println("所在地区:"+area);
    }
    Student(String name){
        this.name=name;
        System.out.println("姓名:"+this.name);
    }
    Student(int age){
        this.age=age;
        System.out.println("年龄:"+this.age);
    }
}

The above is the detailed content of What should you pay attention to when using java construction code blocks?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)