Differences between Java static member variable methods and non-static member variable methods

Source: Internet
Author: User
Tags modifiers

The common methods and member variables here refer to non-static methods and non-static member variables
First, Static is the meaning, is a modifier, can be used to modify variables or methods.

Static member variables have global variables that do not function as static member variables and do not have global variables. Local variables are variables inside the methods of the class
Statically static static member variables are member variables of a class that do not belong to the class and are shared only by the class and by all objects. When you change his value in a class, the value in each object changes.

This means that it is not possible to change his value in the object, only in his class, if he has final (static final) to indicate that it is a constant.
The static variable is statically variable and is loaded when the class is loaded, not the static variable instance object.

The static variable, when it changes its value, is changed by the static variable in the other instance, and the non-static variable is changed only in the object in which it resides, and does not affect other instances.


Example: static int i=1; Here i is a static variable. Specific characteristics are as follows

Static member variables and normal member variables
  • Memory allocation
    Static static, that is, when the class is loaded, this memory is quietly here, it already exists , and ordinary member variables, only the class is instantiated when the memory is allocated.
  • Calling methods
    A member variable requires an object to be called, whereas a static member variable can be called directly from the class, or it can be called directly
    PS: Small question Why can call directly, I see some reference books only say can be called through the class?
     public class demo {static int i= 10; public static void main (string[] args) { int k=i; //direct call int j=demo.i; The System is called through the class. out.println ( "direct call to K:" +k); System. out.println ( "by class to invoke J:" +j);}       
  • Life cycle
    Static member variables begin with the class being loaded, the class is unloaded, and the normal member variable only begins to exist when the class is instantiated, that is, when the object is created, until the end of the object, and life ends.
  • How to Share
    Static member variables are shared across classes, even if they are not objects, and memory is not changed, and ordinary member variables are used individually by each object , that is, you have an int i=1, I will create a new object for the class, he also has an int i, but he can not be equal to 1, can be equal to 5, can be equal to 10, but no matter how much, it does not affect the value of the previous object's I, but the static member variable is not. Static methods and common methods The main method we've been using is static.
    Here I'll just talk about the calling problem
  • Static methods cannot be called directly in a non-static method, but static can be called directly in a non-static method.
  • Static methods can call static member variables, create objects when calling ordinary member variables, and non-static methods can invoke static member variables or ordinary member variables at will.
  • Static methods and common method invocation attributes are the same as above, when static calls to ordinary methods need to create objects, and the normal method is arbitrary.

Usage of static methods

When executing the method in a class, the method does not have any action on the member variable, that is, the operation of the static method is not targeted at the object and is used only for that class. Using this class, you do not need to create an object, but can be implemented directly through the class name. Method Name.


Tool classes-class modifiers, member variable modifiers, method modifiers in PNG Java

Class modifier:

Public (access control), which declares a class as a common class, can be accessed by any object, and the main class of a program must be a public class.

Abstract, declaring a class as an abstract class, without implementing a method, requires a subclass to provide a method implementation.

Final, a class life is final (that is, a non-inheriting class), indicating that he cannot be inherited by another class.

Friendly, the default modifier, only objects in the same package can use such a class.

Member Variable modifiers:

Public (Common access control), which specifies that the variable is public and that he can be accessed by any method of the object.

The private access control specifies that the variable allows access only to methods of its own class, and no other methods in any class (including subclasses) are accessible.

Protected (Protect access Control) specifies that the variable can not be accessed by its own classes and subclasses. This variable can be overridden in a subclass.

Friendly, classes in the same package can be accessed, and classes in other packages cannot be accessed.

Final, final modifier, which specifies that the value of this variable cannot be changed.

Static modifier Specifies that the variable is shared by all objects, that is, all instances can use the variable. Variables belong to this class.

Transient (over modifier) specifies that the variable is a temporary variable that is reserved by the system and temporarily has no special effect.

volatile (volatile modifier) specifies that the variable can be controlled and modified by several threads at the same time.

Method Modifiers :

Public (common control character)

Private (proprietary control) specifies that this method can only be accessed by methods such as its own class, which cannot be accessed by other classes (including subclasses)

Protected (Protected access control) specifies that the method can be accessed by its classes and subclasses.

Final, specifies that the method cannot be overloaded.

Static, which specifies a method that can be activated without instantiation.

Synchronize, a synchronous modifier that, in multiple threads, is used to lock the method to which he belongs before running, to prevent access by other threads, and to unlock after the run is finished.

Native, local modifier. The method body that specifies this method is written outside the program in a different language.

Differences between Java static member variable methods and non-static member variable methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.