Use the static fields and constructors of the class to implement the creation of the object of the class with a Java program

Source: Internet
Author: User

First, the experimental requirements

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?" ".

Second, design ideas

1, create a class Find;

2, define a static integer variable i, so that its record creation of the number of objects;

3, define a function getnum () in the program, so that it returns the measurement value i;

4. Number of output object creation.

Third, the process flow chart

Iv. Source Code

public class Find

{

static int i;

Public Find ()

{

i++; Every object that creates a class I plus 1

}

static int Getnum ()

{

return i;

}

public static void Main (string[] args)

{

Find F1 = new Find ();

Find F2 = new Find ();

Find F3 = new Find ();

Find F4 = new Find ();

Find F5 = new Find ();

System.out.println ("Number of objects created:" +find.getnum ());

}

}

V. Results

Use the static fields and constructors of the class to implement the creation of the object of the class with a Java program

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.