On the difference between static and non static in Java

On the difference between static and non static variables

1. Static modified variables are called class variables, global variables or member variables. When the class is loaded, the member variables are initialized and associated with the class. As long as the class exists, the static variable exists. The non static modified member variable divides the storage space when the object new comes out. It is bound to a specific object. The member variable is only owned by the current object.

2. Static modified variables are loaded in the data sharing area - Method area in memory before the main method. Non static variables are loaded in heap memory only after creating variables.

3. A static variable is divided into a separate storage space, which is not bound with specific objects. The storage space is shared by each object of the class. The static variable value is loaded once in the method area, rather than many times when the object is created. A copy is made for each creation.

4. The object refers to the member variable directly through the class name Called by variable name, an object can only reference an instance variable by object name Called by variable name.

5. call a member variable directly in a class or use a class name. The variable name is called, and the instance variable is called with this or directly.

On the difference between static method and non static method

1. Static is modified in the same way as static. Before the main method, it is loaded into the method area for sharing.

2. This or super keyword cannot be used in static static methods, because static methods are methods that have been loaded before the object is created. They belong to a class, while this and super point to the object of this class or the object of the parent class. Non static methods belong to objects, and this and super can be used in methods.

3. Static method can use object Method name, or class name Method name. Non static methods can only be called when the object is created.

4. The static method is loaded once and shared by all objects. Non static methods are copied as many times as there are objects, and each object can only call its own copied methods.

5. When an object calls a non static method, thread safety is not considered, but when calling a static method, safety should be considered. Because there is only one static method. The method of the object has its own.

6. In the same class, static methods can only access static members in the class. Non static methods can access non static methods (called with a class name, or by creating an object call of this class).

summary

The above is all about the difference between static and non static in Java. I hope it will be helpful to you. Interested friends can refer to: examples of static internal class methods implemented in Java singleton mode, detailed explanations of Java multithreaded forkjoinpool instances, talking about Java annotations and dynamic agents, etc. you can leave a message at any time if you have any questions. Xiaobian will reply to you in time.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>