Java keyword---static

Source: Internet
Author: User

This article analyzes the use of static in Java, mainly five aspects: static member variable, static method, static block, static inner class, static guide package.

The first is a table that says the difference between a static object and a non-static object:

Static objects Non-static objects
Belong Classes collectively have Each instance of a class is independently owned
Memory allocation Fixed on the memory space Subordinate class assignment
Allocation Space Order Allocating static object space preferentially Assign static object space first, same as initialize
   1 static variables, static methods, static blocks   static objects, static methods are decorated with the static keyword on both the original object and the method, which means that the class can invoke these directly without having to instantiate and then call. The advantage is that the data of:     1--static object is globally unique, change all, can guarantee the uniqueness of the data      2--reference convenient, directly with the class name. Method (property) on it, no need to get and set   Of course static methods (variables, blocks) also have corresponding limitations:    No other non-static methods can be called (only static methods are called)     2--can only access static data     3-- This or super  sample code cannot be referenced in any way:   
public class Staticexercise {public static String sky= "Jeyson";    public static string Getmysky (String sky) {return sky;} static {System.out.println ("This is the content in a static block-"); System.out.println ("Static Block:" +getmysky (Sky));} /** * * * * * * * * * * * Static Object Benefits: * The data of the    static object is globally unique, the change is changed, can guarantee the data is unique. *    2--Reference is convenient, direct class name. The method is OK, do not use set and get * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *    Only static data *    3--cannot be referenced in any way by this or super * static Object Test */@Testpublic void TestStatic1 () {System.out.println (Sky); System.out.println ("-----------------"); System.out.println (Getmysky (Sky)); System.out.println ("-----------------"); System.out.println (Statictest.getsms ());}}

  

2 static inner class when a class is closely associated with another class (An outer Class), it is generally used only when the class is used, and can be a static inner class. This can be directly generated as the class loads.     No more instantiation can be instantiated. How to use: Is loaded with the class, calling the direct external class. Inner class. Static object (method) sample code:
Class Statictest{public static string Sms= "To my Sky";p ublic static string getsms () {return SMS;} /** * * * * * * * * * * * * * * * * * * * * * * * Use scenario: *    Internal classes are closely related to external classes, generally only used when using external classes * * * * * * * * * * * * with the    class is loaded, direct external class. Inner class. Static object (method) */static  Class Innerstatic{public static final String mysky= "MY final SKY";}}

 

       @Testpublic  void TestStatic2 () {System.out.println (StaticTest.InnerStatic.MYSKY);}

  

 

2 static Guide If the static method you want to use is in a class other than the current class, you can use a static guide package.    The static method or variable you want to use is the equivalent of being directly visible in the class, without the need for a class name. Static variables are called in the same way. How to use: Import static package name. class name. Static member variable (method) benefits: The static variables and methods that are imported are directly visible in the class, directly using the disadvantages: Some of the code's readability code examples are reduced:
public class Mystaticimporttest {public    static final int number=55;    public static void Getmywords () {    System.out.println ("Hello, this is a static test of the Guide package");}    }

 

Import static Com.generic.mystaticimporttest.number;import static com.generic.mystaticimporttest.getmywords;/** * @ Description: *  static */public class Staticexercise {/** * static guide * * * * * * * * * * * * * * * * * * * * * * * * use: *   direct static import of the appropriate variables and methods: (Import static package name. Class name. static Volume (method) *       import static com.generic.MyStaticImportTest.number;     *       import static com.generic.MyStaticImportTest.getMyWords;     * * * * * Benefits:     *     imported static methods and variables are directly visible in this class, no need to give class name * * * * * * * * * * * * * * * * * * * * to     Some extent reduce the readability of the code */@Testpublic void TESTSTATIC3 () {///static import can be directly with int mynumber=number;getmywords ();}}

  

 

Java keyword---static

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.