Class and object hands-on brain

Source: Internet
Author: User

1. The following code cannot be compiled, where is the error?

Cause: Because the constructor foo () is not defined, if the class provides a custom construction method, the default construction method is no longer provided by the system.

2. Please summarize the rule of Java field initialization according to the output result of the code.

Output Result:

Summarize the rules of Java field initialization: Java field initialization rules the system initializes the Java fields sequentially, and if there are several methods that initialize the same variable. Java assigns the last initialization result to the variable.

3. Hands-on brain: Run the Teststaticinitializeblock.java example, observe the output, and summarize the "Order of execution of static initialization blocks"

Static initialization blocks are executed only once. When you create an object of a subtype, it also causes the execution of the static initialization block of the parent type.

4. Static methods only allow access to static data, so how to access instance members of a class in a static method (that is, a field or method that does not have a static keyword attached)

Please write code to verify your idea

5. Using the class's static fields and constructors, we can track the number of objects created by a class. Write a class that can query it at any time "how many objects have you created?" ”。

Class and object hands-on brain

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.