1 Public Final classThreestooges {2 3 4 /*5 * Stooges is a reference to the Set<string> type, and final qualifies the reference member property stooges to be assigned an initial value and cannot be changed to refer to other similar objects6 * Final only limits the declaration of the reference Stooges cannot be changed, stooges the referenced object can not be changed, by the referenced object itself class definition to determine7 */8 Private FinalSet<string> stooges =NewHashset<string>();9 Ten Publicthreestooges () { OneStooges.add ("Moe")); AStooges.add ("Larry"); -Stooges.add ("Curly"); - the //stooges = new Hashset<string> ();//The final field threestooges.stooges cannot be assigned - } - - /** + * Add an element to the referenced hashset - * Final domain stooges still refers to the HashSet object when the initial value is assigned, and Stooges.add (name); just add an element to the referenced hashset + * @paramname A */ at Public voidAdd (String name) { - Stooges.add (name); - } - - - Public BooleanIsstooge (String name) { in returnstooges.contains (name); - } to + - Public voidprint () { theIterator<string> Iterator =stooges.iterator (); * while(Iterator.hasnext ()) { $System.out.println (Iterator.next () + ",");Panax Notoginseng } - } the + A Public Static voidMain (string[] args) { the + -Threestooges ts =Newthreestooges (); $Ts.add ("ASN"); $ Ts.print (); - } -}
Output:
Moe,
Asn
Curly,
Larry,
Java final domain