Thinking: have you ever thought about a problem, static why it is called static? Because that’s what they call it in books or other articles, right? Hence the name ?????

1. Analyze and understand static state with examples

Well, let’s break it down and fix it today!

In order to solve the above problem, let’s ask another question: if there is stillness, there must be movement. Then who does this “stillness” refer to?

To understand this, you first need to understand the relationship between classes and objects in Java

A class is an abstraction of a class of things with common characteristics, and an object is a concrete instance generated from the class.

Note: Common characteristics here refer to common properties (fields) and common behaviors (methods), and definitely not the same data for these things

For example, Zhang SAN and Li Si belong to students. Here zhang SAN, Li Si is the specific example object; Student is an abstract thing with common characteristics, students have class, name, age, gender and other attributes, so the student is a class.

But the specific example of Zhang SAN, his class is Class 1, name is Zhang SAN, age is 18 years old, gender is male;

Li Si this specific example object, her class is Class 2, name is Li Si, age is 18 years old, gender is female.

Here, class 1 and class 2, 18 years old, male and female are the specific data of specific objects. These data are changed with the changes of different instance objects such as Zhang SAN and Li Si. They are stored with objects

So what is quietness? Static is the data that does not change as the object changes. It is the common data of such things. Note the words in bold and black, which means that these static data are stored with the class, not with the object instance

So static variables are called class variables, and non-static variables are called instance variables.

Continuing the example above, add a field “country” to the student class. Assuming we are only talking about China, then the field “country” can be defined as static, because the value of the country field is China for John, John, and all the other student objects. The value “China” is available to all objects in the student class

So static is what’s common to this class. Static is something that everyone (class) has in common, as opposed to “motion” which changes with the object

I said so much to make it easier for you to understand.

In short: static is with the class, non-static is with the object

So when exactly can you use static?

2. When can STATIC be used?

Because static modifiers have member variables and member methods, the answer to this question is two-fold

When can static variables be defined?

This field can be defined as static when there is data in a class that is common to all objects. For example, the country field in the student class can be static

When can static methods be defined?

A method can be defined as static when no non-static data (the object’s unique data) is used internally

We usually see the tool class XxxUtils is very consistent with the above scene, so we see the tool class is basically static, no new object, direct xxxutils.xxx () call

3, static understanding of other content

With this in mind, the following is not difficult to understand

Static: static keyword

Use of the static keyword: It is a modifier used to modify members (member variables, member methods)

Static features:

1. Load as the class is loaded (that is, as soon as the class is loaded into memory, the static object is loaded as well, while the object does not exist yet)

2. Static exists before objects

3. Shared by all objects

4, can be called directly by the class name, can also be called by the object

Precautions for static use:

Static methods can only access static members (properties and methods), not non-static members. Because the static is there before the non-static is there

2. Non-static methods can access both static and non-static members

3. Because static exists before the existence of an object, the this and super keywords cannot be used in static methods.

Advantages and disadvantages of static:

1. Advantages: For data shared by all objects, there is no need for each object to have a copy. Static data can be used to save space; You can call it with the class name instead of the new object

2, disadvantages: Because static exists with the class, so the life cycle is too long; Access is limited and can only be static

Static and non-static data are also stored differently in memory. We’ll talk more about how they are stored in memory in the next chapter, and what exactly does it do to instantiate an object

The above is my static understanding of Java and summary, if there is any wrong place, welcome to comment, learn and progress together

Tiezi, if you find the article helpful, you can click follow, click like

You can also pay attention to the public number: WX search: “chat 50 cents of Java”, welcome to learn to exchange, pay attention to the public number can receive the blogger’s Java learning video + material, ensure that are dry goods