Initialization of Java variables

Source: Internet
Author: User

Public class Init {

Private int age;//non-static initialization of statements <3>

Private Static String name; Static initialization of statements, first initialize static <1.1>

/** static initialization statements and static initialization modules are static (the same level), who first initialize who in front of * *

{/** dynamic initialization module (non-static initialization module) <4>*/

Double width;

float Price;

System. out. println ("Parent class non-static");

}

Static {/** static initialization module <2>*/

int ID;

String pwd;

name = "admin";//when initializing name and assigning value, do not wait for static initialization module <1.2>

System. out. println ("parent class static");

}

private static String name;/** If the statement is placed under the module, it is the same as in the order <1.1>*/

The/** constructor is not executed until all the member variables have been initialized */

Public Init () {

Char Gender = ' C ';//<5>

System. out. println ("parent construct");

}

Public Static void Main (string[] args) {

New Init();

New Child ();

}

}

/**

* Subclass inherits Parent class, subclass and parent class have both static and non-static and constructed

* 1, initialize the parent class static

* 2, Initialize subclass static

* 3, initialize the parent class non-static

* 4, initialize the parent class construction

* 5, initialize subclass non-static

* 6, Initialize subclass constructs

*

* */

class Child extends init{

{

System. out. println ("sub-class non-static");

}

Static {

System. out. println ("Subclass Static");

}

Public Child () {

System. out. println ("sub-class construction");

}

}

  

Initialization of Java variables

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.