7.static variable Use

Source: Internet
Author: User

 PackageStaticdemo;/** A group of voters vote, each voter is allowed to vote only once, and when the total number of votes reached 100, stop voting * Voter: voter class **/ Public classVoter {Private Static intCount//All voters will change the same data: number of votes    Private Static Final intMax_count = 100;//Maximum number of votes 100 applies to all voters    PrivateString name;//voter's name.     PublicVoter (String name) { This. Name =name; }    //vote     Public voidvotefor () {if(count==max_count) {System.out.println ("Voting is over"); return; }Else{Count++; System.out.println ( This. Name + ": Thank you for voting! "); }    }}
 PackageStaticdemo; Public classTestvoter { Public Static voidMain (string[] args) {voter Zhang=NewVoter ("Zhang San"); Voter Li=NewVoter ("John Doe"); Voter Wang=NewVoter ("Harry");        Zhang.votefor ();        Li.votefor ();                Wang.votefor ();  for(inti=1;i<=97;i++) {Voter v=NewVoter ("V" +i);        V.votefor (); } Voter v101=NewVoter ("v101");    V101.votefor (); }}

7.static variable Use

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.