Hands-on brain and homework after class

Source: Internet
Author: User

1. Why is the following code not compiled? Where did it go wrong?

Cause: Object variables cannot be equal

2. When there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the execution of the block.

3. Static initialization of the execution order of the blocks.

If the static modifier is used when defining the initialization block, it becomes a static initialization block. When static initialization blocks are class-dependent, the system executes static initialization blocks during the class initialization phase, rather than when the object is created.

Static initialization blocks are always executed first than normal initialization blocks. Similar to ordinary initialization blocks, when the system executes static initialization blocks during the class initialization phase, it not only executes static initialization blocks of this class, but also traces back to the Java.lang.Object class. Executes the static initialization block in the Java.lang.Object class first, then executes the static initialization block of its parent class, and finally executes the static initialization block of the class. After this process, the initialization of the class is completed.

4. Please write a class, and at any time you can query it "how many objects have you created?"

Source:

Package Boke;

public class student{

public static void Main (string[] args) {

Information A = new information ();

A.outputinformation ();

Information B = new information ();

B.outputinformation ();

} }

Class information{

static int num=0;

Information ()

{num++;

}

public void Outputinformation ()

{

System.out.println ("You've created a +num+" Object!) ");

}

}

Hands-on brain and homework after class

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.