This article briefly analyzes the usage of static in Java, and mainly five aspects: static member variables, static methods, static blocks, static internal classes, and static packages.
First of all, let’s talk about the difference between static objects and non-static objects in a table:
| Static Objects | Non-static objects | |
| Attribution | Classes have | Each instance of the class is owned independently |
| Memory allocation | Fixed in memory space | Affiliate class assignment |
| Allocation of space order | Prioritize the allocation of static object space | Priority allocates static object space, and the same is true for initialization |
1 Static variables, static methods, static blocks
Static objects and static methods are decorated with static keywords on the original objects and methods, indicating that the class can call these directly without instantiating them before calling them. The benefits are:
1--The data of static objects is globally unique, and it can ensure the uniqueness of the data.
2--Easy to quote, just use the class name and method (properties) directly, no need to get and set
Of course, static methods (variables, blocks) also have corresponding restrictions:
1-No other non-static methods can be called (only static methods can be called)
2--Only access static data
3--This or super cannot be referenced in any way
Sample code:
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 the static block--");System.out.println("static block: "+getMySky(sky));}/*** *** *** Benefits of static objects: * 1-The data of static objects is globally unique, and it can be ensured that the data is unique. * 2--The reference is convenient, just use the class name and method, without set and get* ***Assign space order: * First allocate static object space and then non-static. The initialization order is the same* ***static final modified member variables and member methods can be understood as global variables (there is no concept of global variables in java)* *** Limitations of static objects, methods, and blocks: * 1-Only call other static methods* 2-Only access static data* 3-This or super* cannot be referenced in any way 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 one class is closely related to another class (external class), and the class is usually only used when an external class is used, the class can be used as a static inner class. This can be generated directly as the class is loaded. No need to instantiate it.
Usage method: loaded with the class, and call direct external class, internal class, static object (method)
Sample code:
class StaticTest{public static String sms="to my sky";public static String getSms(){return sms;}/*** Static inner class* ***Usage scenario: * The inner class is closely related to the outer class, and this class is generally used only when using the outer class* ***Use* It is loaded with the class, directly external class. Internal 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 package
If the class where the static method you want to use is not in the same package as the current class, you can use the static delivery package. In this way, the static method or variable you want to use is equivalent to being directly visible in the class, and there is no need to call the class name or static variable.
How to use:
import static package name.class name.static member variable (method)
benefit:
The imported static variables and methods are directly visible in this class and are directly used
harm:
To some extent, the readability of the code is reduced
Code example:
public class MyStaticImportTest {public static final int number=55;public static void getMyWords(){System.out.println("hello , this is a test of static lead package");}} import static com.generic.MyStaticImportTest.number;import static com.generic.MyStaticImportTest.getMyWords;/*** @Description:* Static*/public class StaticExercise {/*** Static lead package* ***Usage method: * Direct static import corresponding variables and methods: (import static Package name. Class name. Static member variable (method) * import static com.generic.MyStaticImportTest.number;* import static com.generic.MyStaticImportTest.getMyWords;* *** Benefits: * The imported static methods and variables are directly visible in this class, no need to give the class name* *** Disadvantages* To a certain extent reduce the readability of the code*/@Testpublic void testStatic3(){//After static import, you can use int myNumber=number;getMyWords();}}The above is a brief analysis of the usage of static keywords in java introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!