Home >Java >javaTutorial >A brief introduction to the difference between static and non-static in Java
This article mainly introduces the related content of static and non-static in Java. The editor thinks it is very good. I share it with everyone here. Friends who need it can refer to it.
About the difference between static and non-static variables
1. Static modified variables are called class variables or global variables or member variables. When the class is loaded At that time, the member variables are initialized and associated with the class. As long as the class exists, the static variable exists. Non-static modified member variables divide the storage space when the new object comes out, and are bound to the specific object. This member variable is only owned by the current object.
2. Variables modified with static are loaded in the data sharing area in the memory ------- method area before the main method when loading, while non-static variables are loaded before main method. , variables must be created before they are loaded into the heap memory.
3. A static variable divides a storage space separately and is not bound to a specific object. The storage space is shared by each object of the class. Static variable values are loaded once in the method area, while non-static variables are loaded many times when creating objects. A copy will be made each time it is created.
4. When an object refers to a member variable, it is called directly through the class name.Variable name. When an object refers to an instance variable, it can only be called through the object name.Variable name.
5. When calling member variables in a class, call it directly or call it in the form of class name.variable name. For instance variables, use this or call it directly.
About the difference between static methods and non-static methods
1. The static modified method is also the same as static. It is loaded into the method area before the main method for shared use.
2. You cannot use this or super keywords in static static methods, because static methods are methods that have been loaded before the object is created. They are methods of the class, and this and super point to are objects of this class or objects of the parent class, non-static methods belong to objects, and this and super can be used in methods.
3. The static method can be called using object.method name or class name.method name. Non-static methods can only be called after 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 copy method.
5. When an object calls non-static methods, thread safety issues are not considered, but when calling static methods, security issues must be considered. Because there is only one copy of the static method. The object's methods have their own.
6. In the same class, static methods can only access static members of the class. Non-static methods can access non-static methods (call using the class name, or create an object of this class).
Summarize
The above is the detailed content of A brief introduction to the difference between static and non-static in Java. For more information, please follow other related articles on the PHP Chinese website!