Track the number of objects created by a class

Source: Internet
Author: User

 PackageDemo01; Public classdemo01 {Private Static intN;  Publicdemo01 () {n++;//you can give n plus 1 for a second object.        }         Public Static intGetnum () {returnN//return n Value        }         Public Static voidMain (string[] args) {//TODO Auto-generated method stubsDemo01 t1=Newdemo01 (); DEMO01 T2=Newdemo01 (); demo01 T3=NewDemo01 ();//Creating ObjectsSYSTEM.OUT.PRINTLN ("Total number of objects:" +demo01.getnum ()); }}

Results:

Track the number of objects created by a 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.