Determine the number of class objects

Source: Internet
Author: User

The static field of the package class, the number of public class statistics objects {
Static Class Employee
{
static int num;
Public Employee () {
num = num + 1;

}
int sum1 () {
return num;
}

}

public static void Main (String []args) {
Employee A = new Employee ();
Employee B = new Employee ();
Employee C = new Employee ();
Employee D = new Employee ();
Employee E = new Employee ();
Employee F = new Employee ();
SYSTEM.OUT.PRINTLN ("You have created a +employee.num+" object ");

}
}

Because after the constructor of the class has been modified, the initialization object calls only the modified function, so the object is not initialized and the function's arguments should be added so that the object is initialized.

The execution order of static initialization blocks:

Static initialization blocks are performed at the highest priority and can only be performed once. The constructor executes after it, and the constructor is called once without generating the object once.
Non-static initialization of block constructors is performed before. The constructor is the initialization of the object.

Determine the number of class objects

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.