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