A deep understanding of Java's static (combined with C-language thinking)

Source: Internet
Author: User

1  Public classStatictest {2 3     4String X = "I am a non-static variable";5     6     7     Static intButterfly =0;8     StaticString staticx = "I am a static variable" ;9     StaticString staticz= "I am also a static variable";Ten      One      A statictest () { -System.out.println ("I Am a Builder"); -     } the      -      -      -      Public Static voidMain (string[] args) { +Statictest this1 =NewStatictest ();//new out of this object is just for the following FANGFA2 (), but it doesn't matter if you use a constructor in a static block. -         //System.out.println (FF3); Error, the variable in the static method is also a temporary variable, not a global +         //System.out.println (Staticy); Error, indicating that even if the amount defined in the static block is a temporary variable, it is not global, nor is it a class A         //THIS.FANGFA2 (); Error, stating that this is an object and cannot be used in the static method at this1.fangfa2 (); -FANGFA4 ();//simply want to explain the need not to use THIS1 reference -          -     } -  -      in     Static{ -Statictest This2 =NewStatictest ();//just to show that you can make a new object in the static block earlier, in order to run the constructor toString staticy = "Staticy"; +         //System.out.println (x); wrong - System.out.println (staticy); the         //this.fangfa1 (); This is wrong, because this is the object that represents itself, is an object value, and static is loaded before the object *FANGFA1 ();//describes static method methods that can be called by a passive block $ //if (true) {} pair, the static block can be used as a non-name, cannot be called a special method, its essence is a static method, but also equivalent to the main method can bePanax Notoginseng //run, and run first than main -     } the      +     Static voidfangfa0 () { ASystem.out.println ("I Am a Butterfly"); the}//This method just wants to show that I like butterflies, no specific use +      -      $      $      -     Static voidfangfa1 () { - System.out.println (staticx); the         //System.out.println (X); wrong, because static methods cannot call non-static quantities - System.out.println (Fangfa3 ());Wuyi         //System.out.println (FF3); error, FF3 even though it is defined in a static method, it is still a temporary variable, but if you precede it with static, it will not be wrong. the          -          Wu     } -      About     StaticString Fangfa3 () { $String FF3 = "I am fangfa3"; -         returnFF3; -          -     } A      +     Static voidfangfa4 () { the fangfa0 ();  -     } $      the      the     voidfangfa2 () { theSystem.out.println (Staticz);//static methods can call static quantities the          This. X = "2"; -     } in      the      the      About } the /*Printing results:*/ the //I'm the constructor . the //Staticy + //I am a static variable - //I'm fangfa3 . the //I'm the constructor .Bayi //I'm also a static variable the //I'm a butterfly . the  -  - /*Summary process: 1, static loading and initialization: The static amount (variables and methods) initialization, the initialization of the method means that the variable name is assigned to the pointer; the initialization of the variable is the * Assign a general data type directly to this variable the * 2, static execution: Then the contents of the static block execution, it is equivalent to the main method is executed first, is the static execution of the portal like "main", the * 3, non-static loading and initialization: the rotation of non-static variables and methods of loading and its assignment the * 4, the implementation of the constructor - * 5, non-static execution: Execution of the Main method the * Note: the assignment and invocation of the method is 2 concepts, the assignment of the method is the assignment of the address pointer to the indirect variable of the method name, and the method call is called this method the * Note: There are non-static blocks not mentioned the * Note: Execute and load and initialize these 3 concepts must distinguish between94 * Note: Static is common to all objects of this class, that is, only these 1, not the static amount is each object itself has one, like the first new variable X,THIS2. X and this1.x are not the * Same One the * Note: Because the static amount is common, it is better to call it directly before adding the object name, which helps to understand the concept. the  */


A deep understanding of Java's static (combined with C-language thinking)

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.