Static keyword Usage

Source: Internet
Author: User

The static keyword in Java can be used to decorate:

1. Attribute: Indicates that the property variable is created and initialized when the class is loaded, and the class loading process occurs only once, so the static variable is created only once

2. Method: The static method is the public method of the class, which can be accessed directly with the ' class name. ' Method name ', the static method cannot access the non-static members in this class, the subclass is not

The static method of the parent class is overwritten as non-static and vice versa. In addition, the This keyword cannot be used in static methods

3. Code block: Static initialization code block, executed once when the class is loaded, can be used for initialization of class

Specific examples:

1) In the following example, the static variable, Staticvar, is created and initialized only the first time the class is loaded, and no matter how many class objects are created, the variable is no longer

Load, so each time the original base is incremented by 1, and the instance variable Instancevar is created only when the class object is created and in the constructor

Add 1 To create an Instancevar instance variable for each object created. Note: Accessing Instancevar in a static method will error.

2) in the Usestatic class, class loads are initialized in the following order: Static variable---static code block

1  Packagecom.pattywgm.a_fundamental;2 3 Importorg.junit.Test;4 5 /**6  * 7  * @authorPatty8 * @time 2014-12-49 * @todo to examine the use of the static keywordTen  */ One  Public classStatictest { A       //default is 0, do not specify an initial value -      Static intStaticvar;//static variables, loaded at class initialization, loaded only once -      intInstancevar;//instance Variable the  -       Publicstatictest () { -staticvar++; -instancevar++; +System.out.println ("staticvar=" +staticvar+ "instancevar=" +Instancevar); -            +     } A       at       Public voiddosth () { -System.out.println ("Do something ..."); -      } -       -       Public Static voiddosthstatic () { - //System.out.println (Instancevar);//static references cannot be made to non-static field Instancevar inSystem.out.println ("Do something static ..."); -      } to      + @Test -      Public voidteststatic () { the           //unit tests first create an object of a class, and then call the test method *           Newstatictest (); $      }Panax Notoginseng      -      Public Static voidMain (string[] args) { the         //statictest statictest;  +System.out.println (Staticvar);//The object is not created at this time, and the Staticvar value is not increased, still 0 A          //for each Statictest object created, the Staticvar value increases 1,instancevar value by 0 plus 1 theStatictest statictest1=NewStatictest ();// +Statictest statictest2=Newstatictest (); -Statictest statictest3=Newstatictest (); $System.out.println ("Instancevar (whole) is:" +Statictest3.instancevar); $          intinstancevar=200;//Local Variables First -System.out.println ("Instancevar (local) is:" +Instancevar); -System.out.println ("Staticvar (whole) is:" +Staticvar); the          intstaticvar=500;//Local Variables First -System.out.println ("Staticvar (local) is:" +Staticvar);Wuyi           the      } -}//End Statictest Wu /** -  *  About  * @authorPatty $ * @time 2014-12-4 - * @todo Examine the static variable/method/code block - * Class load is loaded in order: Static variable, static code block, then static method is called -  */ A classusestatic{ +      Public Static intA=3; the     Static intb; -      $     Static voidMethodintc) { theSystem.out.println ("C:" +c); theSystem.out.println ("A:" +a); theSystem.out.println ("B:" +b); the     } -     //code block in     Static{ theSystem.out.println ("Static block ..."); theSystem.out.println ("a=" +a+ "b=" +b);//3 0 AboutB=a*5; theSystem.out.println ("a=" +a+ "b=" +b);//3 the     } the      +      Public Static voidMain (string[] args) { -Method (24); theSystem.out.println (Statictest.staticvar);//0Bayi     } the}//End Usestatic the  - classStaticdemoextendsstatictest{ -     Static intSTA; the     intSTB; the      PublicStaticdemo () { the         //super (); thesta++; -stb++; theSystem.out.println ("Sta=" +sta+ "stb=" +STB); the     } the     //Dosth cannot be declared here as a static method, that is, a subclass cannot overwrite a non-static method of the parent class with static94      Public voiddosth () { the         Super. Dosth (); theSystem.out.println ("Sub do Something"); the     }98     //The static modifier must be added here, because instance methods cannot overwrite static methods in Statictest About      Public Static voiddosthstatic () { -Statictest.dosthstatic ();//static methods are not available with the Super keyword and are accessed directly with the parent class name101System.out.println ("Sub do something Static");102     }103     104      Public Static voidMain (string[] args) { theSystem.out.println ("Sta=" +STA);106         NewStaticdemo (). Dosth ();107 dosthstatic ();108     }109 } the 111 ///:output: the /*statictest113 * Teststatic the staticvar= 1 instancevar= 1 the staticvar= 2 instancevar= 1 the * Main117 0118 staticvar= 1 instancevar= 1119 staticvar= 2 instancevar= 1 - staticvar= 3 instancevar= 1121 Instancevar (whole) is:1122 Instancevar (local) is:200123 Staticvar (whole) Is:3124 Staticvar (local) is:500 the      126 *staticdemo127 sta= 0 - staticvar= 1 instancevar= 1129 sta= 1 stb= 1 the Do something ...131 Sub do something the Do something static ...133 Sub do something static134  */135 //:~~~
View Code

Static keyword Usage

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.