1. Properties declared with the static keyword are global properties
Before you specify city with the static keyword, if you need to change the cities of the Tom,jack,mary to Beijing, you need to re-declare the three-time object to be Beijing
1 PackagePackageone;2 classpeople{3 String name;4String City = "Shanghai";5 Publicpeople (String name) {6 This. Name =name;7 }8 Public voidShowinfo () {9System.out.println ("Name:" +name+ "," + "City:" +City );Ten } One } A Public classStaticdemo { - - Public Static voidMain (string[] args) { thePeople p1 =NewPeople ("Tom"); -p1.city = "Beijing"; - p1.showinfo (); -People p2 =NewPeople ("Jack"); +p2.city = "Beijing"; - p2.showinfo (); +People p3 =NewPeople ("Mary"); A //if the city property of the P3 object is not declared to be Beijing, its city is still Shanghai. at p3.showinfo (); - } -}
When you specify city with the static keyword, you can make changes to three people cities by simply setting up town = "Beijing"
1 PackagePackageone;2 classpeople{3 String name;4 StaticString City = "Shanghai";5 Publicpeople (String name) {6 This. Name =name;7 }8 Public voidShowinfo () {9System.out.println ("Name:" +name+ "," + "City:" +City );Ten } One } A Public classStaticdemo { - - Public Static voidMain (string[] args) { thePeople p1 =NewPeople ("Tom"); -p1.city = "Beijing"; - p1.showinfo (); -People p2 =NewPeople ("Jack"); + p2.showinfo (); -People p3 =NewPeople ("Mary"); + //the city property of the P3 object is not declared to be Beijing, but its city becomes Beijing. A p3.showinfo (); at } -}
2. The properties and methods declared with the static keyword can be called directly from the class name (the code is a bit more complex to interpret the static application, and is a simple use of the interface)
1 PackagePackageone;2 3 InterfaceUSB {4 voidstart ();5 voidstop ();6 }7 8 classC {9 Public Static voidWork (USB u) {Ten U.start (); OneSystem.out.println ("At Work"); A u.stop (); - } - } the - classUsbdiskImplementsUSB { - @Override - Public voidstart () { +System.out.println ("U disk starts working"); - } + A @Override at Public voidStop () { -SYSTEM.OUT.PRINTLN ("USB Stick stops working"); - } - } - - classPrinterImplementsUSB { in @Override - Public voidstart () { toSYSTEM.OUT.PRINTLN ("Printer starts working"); + } - the @Override * Public voidStop () { $SYSTEM.OUT.PRINTLN ("Printer stopped working");Panax Notoginseng } - } the + Public classInterfacetest { A Public Static voidMain (string[] args) { the //calling the work method directly from the class name +C.work (NewUsbdisk ()); -C.work (NewPrinter ()); $ } $ -}
3. Note: "When using the static method, you can access only the properties and methods of the static declaration, not the properties and methods of the static declaration." Instead of a static declaration, it is possible to invoke the properties or methods of the static declaration
1 PackagePackageone;2 //because Bo master my level of limited reference to other people's code case, but because of his code has a big error, after debugging successfully quoted, is your own code it ~ hehe3 classpeople {4 PrivateString name;5 Private intAge ;6 7 PublicString GetName () {8 returnname;9 }Ten One Public voidsetName (String name) { A This. Name =name; - } - the Public intGetage () { - returnAge ; - } - + Public voidSetage (intAge ) { - This. Age =Age ; + } A at //defining the Country property with static - Private StaticString country = "China"; - - //define the static method, modify the static property - Public Static voidsetcountry (String c) { -Country =C; in } - to //get the static property + Public StaticString getcountry () { - returnCountry; the } * $ //Assigning a value to a property by constructing a method (Initialize operation)Panax Notoginseng PublicPeople (String name,intAge ) { - This. Name =name; the This. Age =Age ; + } A the Public voidinfo () { +System.out.println ("Name:" + getName () + "Age:" +getage () + "City:" +country); - } $ } $ - Public classStaticdemo { - Public Static voidMain (String args[]) { thePeople per1 =NewPeople ("Zhang San", 20); -People per2 =NewPeople ("John Doe", 21);WuyiPeople per3 =NewPeople ("Harry", 23); theSystem.out.println ("--------------------before modification"); - per1.info (); Wu per2.info (); - per3.info (); AboutSystem.out.println ("--------------------after modification"); $ //call the method directly with the class name to modify the contents of the static property, just because country is a static global variable and does not require everyone to modify the nationality -People.setcountry ("USA")); - per1.info (); - per2.info (); A per3.info (); + } the}
Java-static keywords