04 of the hands-on brain in Java

Source: Internet
Author: User

1. If the class provides a custom construction method, it will cause the system to no longer provide a default construction method

2, code output results:100

300

The rule of field initialization is that if the newly created variable is not assigned an initial value, the default constructor is automatically called to assign the initial value, and if it is assigned, it is assigned.

3. Operation Result

static initialization block of Root

static initialization block for Mid

static initialization block of the Leaf

common initialization block for Root

The parameterless constructor of Root

Normal initialization block for Mid

parameter-free constructors for Mid

The parametric constructor for Mid, with parameter values:Java Initialization sequence Demo

ordinary initialization blocks of the Leaf

Execution the Leaf's constructor

Parse: Call the static initialization block of the parent class first, call the static initialization block of the subclass, call the static initialization block after calling the normal initialization block and constructor of the parent class, call the normal initialization block and the constructor of the subclass respectively.

4. An instance member who wants to access a class in a static method can define a static object in the method so that the static method can access the instance members of the class.

5 . See how many objects have been created

Code implementation:

Public class Objictnumber

{

int number;

Public Objictnumber ()

{

++number;

System. out. println (" currently created " +number+ " objects ");

}

Public Static void Main (String[]args)

{

@SuppressWarnings ("unused")

Objictnumber obj=New objictnumber ();

}

}

04 of the hands-on brain in Java

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.