Writing in the front

Static Java static Java static Java static Java static A few days ago, I suddenly remembered the related problems about static in my earlier work. Let me summarize myself.

Static is used to modify variables, methods, code blocks, and packages. Static is used to modify variables, methods, and packages.

start

When you think of static, what do you think of? ! The main method is static. The main method is static. If you’re not sure, read on in doubt.

Static modifiers

What does static mean? The word static is used to describe a variable that is globally static.

Static variables are also called class variables. Static variables are owned by that class. A little confused about what that means? The static keyword can only be defined in a class, not in a method. See the screenshot below:

As you can see from the figure above, it is clear that you cannot define static variables in a method, even if you add static to the method.

Since the static modified variable is owned by the class, it is possible to call the static modified variable directly from the class without manually instantiating the class (i.e., the class name). Static variables). If necessary, you should also pay attention to variables such as public or private, which we won’t discuss here.

What is the use of static modified variables

We used static variables in our project earlier. When we started the project, we loaded some important system data, such as infrequently modified data, dictionary data, etc., and assigned static variables to a class. If it is not assigned a second time, its data will remain the same as the first assignment until the server stops, facilitating data calls. There are other applications, and we’re not going to study them.

There are several concepts about variables:

  • Variables defined outside of methods, code blocks, and constructors are called member variables.
  • Variables defined in methods, code blocks, and constructors are called local variables.
  • Variables defined in method parameters are called parameters.

Static modifier

A method that is static is defined as a method that has the static keyword. Static methods can be called from the class itself, without creating any objects. Static methods are called from the class itself.

As you can see in the figure above, “staticFunction” is a static method decorated static, so you can use “class name” directly. Method name “, this call is actually and before we said “class name. Variable names are called in the same way.

Now that we’re done with the static modifier, you should probably know why the main method is static. It is obvious that the main method is static because the program does not create any objects when it executes the main method, so it can only be accessed by the class name.

So by using this static modifier on main, we can actually extend the technique, which is the singleton pattern. If you are familiar with the singleton pattern, you will know that the singleton pattern is related to the method of static modification.

Note the static modifier method

  • The first bad thing is that you don’t have to create an object, you just create the class name. The variable name can be called and accessed.
  • Non-static methods cannot be called from the inside of static modified methods.
  • Inside a non-static method is the ability to call static static methods.

Static decorates a code block

First of all, code blocks are divided into two types, one is to use {} code block, and the other is to use static{} code block, we call “static code block”. Static code blocks can be placed anywhere in a class, and there can be more than one static code block. When a class is first loaded, it is executed in the order of static blocks. Each static block can be executed only once, and only once at class load time.

hi static1 run
hi static3 run
hi static2 run
staticFunction is run
Copy the code

As you can see in the code and result above, static blocks of code can be multiple, placed anywhere, and executed only once at class load time, and from top to bottom.

Static is used as a static inner class

Before saying static inner class, we first popularize the classification of inner class, which is divided into ordinary inner class, local inner class, static inner class, anonymous inner class four. Static inner classes can contain either static or non-static member variables, but a non-static inner class cannot declare a static member variable.

Static inner class has a lot of role, because of the static inner class instance creation requires external class object references, so the static inner class object must be created depends on external instance of the class, and of a static inner class instance creation simply relying on external classes, and because of the static inner class object holds the external class object references, So a non-static inner class can access non-static members of an outer class, whereas a static inner class can access only static members of an outer class.

Since static inner classes are rarely used, we won’t go into too much detail. If you’re interested, you can take a look at Java inner classes.

Static import package

First of all, when we use a class, such as ArrayList, we need to import java.util.ArrayList, so our code is “import java.util. It couldn’t be simpler. So what is a static import package? To import a static package, use “import static” to import static methods or variables from a class or package.

First I define a class called “StaticDemo” where I define a static variable “I” and a static method “test”. Then in the second class, “StaticTest”, I imported all the static variables and methods of “StaticDemo” as “import static”, and I could call them directly. Seeing that there seems to be nothing special about this feels like an extra move to just use the “class name”. Variable “or” class name. Method “not on the line, right you say the right, we are just talking about what is static import package.

Static The life cycle of a variable

Static variables have the same life cycle as classes. They are created when the class is loaded and destroyed when the class is destroyed. A normal member variable has the same life cycle as the one to which it belongs. In fact, this is where the article started and the purpose of the static variable that I was talking about is to take advantage of this.

Static and serialization

Java serialization is simply about keeping the state of various objects (that is, instance variables, not methods) in memory and reading the saved object state back out. Although you can save object States in various ways of your own, Java gives you a mechanism for saving object states that should be better than your own, and that is serialization.

Static variables cannot be serialized. Static members do not belong to an object instance and are class-level, so they cannot be serialized.

Static is often used for log printing

private static final Logger LOG = LogFactory.getLogger("StaticTest.class");
Copy the code

The above code I think you are often seen, print the log. Of course, there is no problem to remove “final” and “static”, both can run, both can print logs. But it’s clear that after removing “final” and “static”, there will be a “LOG” for every instantiated object of a class. For instance objects that create 100, 1000 or more classes, there will be 100, 1000 or more “Logger” objects. It will definitely lead to a waste of resources. Therefore, the “Logger” object is usually declared static to reduce memory footprint.

The last surprise question is “Is the constructor of a class static?”

I don’t know, but I have a vague memory of one of my teachers telling me that “constructors don’t have a static modifier, but they are static.” So why can’t constructors have a “static” modifier? A constructor is used to initialize an object and to generate an instance, so it cannot be used to instantiate an object. ! This…? !!!!!!!!! , EMMM!! ????? , interested friends can study by themselves.