Java object initialization

Source: Internet
Author: User

Java object

Class {

{Show (0 );}

Int x = 1;

{Show (1 );}

A () {x = 2 ;}

Void show (int label ){}

}

Class B extends {

{Show (2 );}

Int y = 1;

{Show (3 );}

B () {y = 2 ;}

Void show (int label ){

System. out. println (label + ": x =" + x + "y =" + y );

}

}

Public class C {

Public static void main (String [] args ){

New B (). Show (4 );

}

}

Output:

========================================================== ==========

0: x = 0 y = 0

1: x = 1 y = 0

2: x = 2 y = 0

3: x = 2 y = 1

4: x = 2 y = 2

Describes the initialization sequence:

0. Allocate storage space for the base class and sub-class members

1. Initial values assigned to base class members

2. base class Constructor

3. Assign initial values to subclass members

4. Subclass Constructor

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.