Beginner Java, in the face of this static modifier, Leng two days of time, still pondering today, now will realize the things recorded:
1, the static modifier, the content (variables, methods, blocks of code that have been temporarily learned about these three), is referred to statically, static content (static variable, static method, static code block) 2, static content is related to the content of the class. Explanation: A static variable will be allocated memory space when the class is loaded, and will only be allocated once, that is, the static variable is shared within the class, everyone can call to access it, as long as in a call to change its value, then all the internal values of the call will be changed. A static method is also assigned a portal address when the class is loaded, and all methods can invoke it (without creating the object). Static code blocks are executed once when the class is loaded and only once. 3, here for static content, introduce the non-static content, as a comparison. Non-static variables must be created after the object is not allocated space in memory, this time can be called, non-static methods will be created after the object to take advantage of the object call (that is, "object name. Method ()" call), non-static code blocks are executed once each time the object is created, and is a priority. These non-static content is the object-related content that needs to be used after owning the object, which is the most basic difference from static content. 4, now talk about the specific call Details: all static methods or variables can be called by static or non-static methods in this class or heterogeneous in the format "class name. Variable name" and "class name. Method Name ()", even when called in this class, you can omit the "class name." And call it directly, and stick to "a change, change everywhere" 's Principles. 5. All non-static variables or methods can be static or non-static methods in this class or heterogeneous to "first create an instance object of the class, with an ' object name. Variable name ' and ' object name '. The format invocation of the method name () ' ", even in non-static methods in this class, can be called directly (warning: The direct call here is different from the variable or method called by the ellipsis call, they are stored in memory in different locations, the contents of the new object are in the object area, And the direct call is directly to the property of the class to operate the value of this is the test code, (can be copied directly into your editor in the trial Run): (part of the code to test the content, you can remove comments to see if the correct)
1/** 2 * Do not distinguish between this class or the heterogeneous, the static member is called must be called at the class name point, non-static members must create a tired instance to call 3 * Difference: 4 * 1, static member calls: 5 * in this class (static and non-static methods) call can omit the class name point, directly called (here, either Where the call changes the value of the property, all calls change, because they point to the same address in memory. 6 * 2, non-static member invocation: 7 * Call in a non-static method of this class can omit the creation of the instance object, direct call (so that writing will not error, But unlike creating objects with objects that point to the content they point to, they point to different addresses in memory. 8 */9 PackageCOM.TEST;10 public classstatictest{12 int a = 1; static int B = 2; 14//Non-static method (void)Testa () {System.out.println (non-static method in a Class)); 17//test static and non-static variables called by the same class of static method (the call of the method is the same as the variable) 18 19//Call this class static variable (omitted the class name point fetch) statictest.b = 12; System.out.println (b);//Omit the class name from 22 23//Call this class non-static variable 24//Here you can invoke the a variable in the way you create an instance object, but in this case a is different from calling a directly, which refers to the property A of the new object. The latter is a property of the class//statictest m = new Statictest ();//M.A = 11;27//a = m.a;28 a = 11; 29System.out.println (a); 30 31//Call a heterogeneous static variable (this will be an error when calling directly) +//b2 = 14;//Error QITA.B2 = 14; 34System.out.println (QITA.B2); 35 36//Call a heterogeneous non-static variable (the direct call here will be an error) PNS//A2 = 13;//error Qita x = newQita (); x.a2 = 13; 40System.out.println (X.A2); 41}42 static voidTestb () {System.out.println ("Static methods in a class")); 44//test static and non-static variables called by the same class of static method (the call of the method is the same as the variable) 45 46//Call this class static variable statictest.b = 22System.out.println (b);//Omit the class name point 49 50//Call this class non-static variable statictest y = new statictest (); Tem.out.println (Y.A); 54 55//Call a heterogeneous static variable, qita.b2 =58, System.out.println (QITA.B2), 59//Call a heterogeneous non-static variable, Qita x = New Qita (); x.a2 = System.out.println (x.a2); }64 public static void main (string[] args) { 65///static method calls the non-static method in this class Statictest z = new statictest (); Z.testa (); 68 69//static method called in this class in statically Testb (); 71 c14>}72 }73 class qita{74 int a2 = 3; static int b2 = 4; void Testa2 () {$ System.out.println ("Non-static method in Class II"); }79 static void testb2 () {System.out.println ("static method in Class two"); Bayi }82}
Learning experience with static modifier