Java class initialization block

Source: Internet
Author: User

Tag: class []. SH Change www. Variable assignment cannot initialize tar statically

A class object is initialized in Java, and the data is assigned by initializing a fast or constructed method. There are several execution codes associated with them:

    • Static initialization blocks
    • Initialize block
    • Construction method
Static initialization blocks

Static initialization blocks are only executed once when the class is loaded, and static initialization blocks can only assign values to static variables and cannot initialize normal member variables.

Non-static initialization block

Non-static initialization blocks are executed once each time the instance object is initialized, and any variable can be assigned a value.

Construction method

Called every time the instance object is initialized.

Focus: Order of execution
    1. Executes a static initialization block (which is no longer called) when the class is loaded.
    2. Each time the instance object is initialized: a non-static initialization block is executed before the construction method is executed.

If the concept is not easy to understand, see the following example:

public class Initializationclass {
String name; Declaring variable name
String sex; Declaring variables Sex
static int age;//declares a statically variable age

Construction method
Public Initializationclass () {
SYSTEM.OUT.PRINTLN ("Initialize name by construction method");
Name = "Tom";
}

Construction method
Public Initializationclass (String name) {
SYSTEM.OUT.PRINTLN ("Initialize name by constructor method of the parameter");
THIS.name = name;
}

Construction method
Public Initializationclass (int.) {
SYSTEM.OUT.PRINTLN ("Change static Initializationclass.age by construction method");
Initializationclass.age = age;
}

Construction method
Public Initializationclass (string name, string sex) {
SYSTEM.OUT.PRINTLN ("Initialize name Age by construction method");
THIS.name = name;
This.sex = sex;
}

Initialize block
{
SYSTEM.OUT.PRINTLN ("Initialize sex by initialization block");
Sex = "male";
}

Static initialization blocks
static {
SYSTEM.OUT.PRINTLN ("Initialize age by static initialization block");
Age = 20;
}

public void Show () {
System.out.println ("Name:" + name + ", Gender:" + Sex + ", Age:" + ages ");
System.out.println ("------------------------------");
}

public static void Main (string[] args) {

Creating objects
Initializationclass obj1 = new Initializationclass ("Qiuhua");
Obj1.show ();

Initializationclass obj2 = new Initializationclass (10);
Obj2.show ();

Initializationclass obj3 = new Initializationclass ("Jing", "female");
Obj3.show ();
}
}

  

Initializing the age with static initialization blocks
Initialize the sex by initializing the Block
Initialize name by parameter construction method
Name: Qiuhua, Gender: Male, Age: 20
------------------------------
Initialize the sex by initializing the Block
Changing static Initializationclass.age by construction method
Name: null, Gender: Male, Age: 10
------------------------------
Initialize the sex by initializing the Block
Initialize the name age by constructing the method
Name: Jing, Gender: Female, Age: 10
------------------------------

Ext.: https://www.cnblogs.com/Berryxiong/p/6245100.html

Java class initialization block

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.