Home  >  Article  >  Java  >  Detailed introduction to Java keyword static

Detailed introduction to Java keyword static

黄舟
黄舟Original
2017-03-16 09:52:241693browse

static means "global" or "static", is used to modify members variables With member methods, static code blocks can also be formed, but There is no concept of global variables in the Java language.

Member variables and member methods modified by static are independent of any objects of the class. That is, it does not depend on a specific instance of the class and is shared by all instances of the class. As long as this class is loaded,

The Java virtual machine can determine the method area in the runtime data area based on the class name. Find them. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.

The static member variables and member methods modified with public are essentially global variables and global methods. When an object of its class is declared, a copy of the static variable is not generated, but All instances of a class share the same static variable.

static variable can be preceded by private modification, indicating that this variable can be used in the static code block of the class, or in other static member methods of the class (of course it can also be used in non-static member methods used in --- nonsense), but it is important not to directly reference it through the class name in other classes. In fact, you need to understand that private means access permission restriction, and static means it can be used without instantiation, which is much easier to understand. The effect of adding other access rights keywords in front of static is also the same.

Some characteristics of static:

(1) Static variables are also called static The difference between variables, static variables and non-static variables is that static variables are shared by all objects and have only one copy in memory. It will be initialized when and only when the class is first loaded. Non-static variables are owned by the object and are initialized when creating the object. There are multiple copies, and the copies owned by each object do not affect each other.

(2) statice has two functions: first, it only wants to allocate a single storage space for a specific domain, regardless of how many objects are created, or even no objects are created; second, , hope that a method is not associated with any object containing it, that is, any method in it can be called without creating any object.

(3) Any member variables and member methods modified by statice are independent of any object of the class. It does not depend on any object of the class and is shared by all instances.

(4) Static variables have only one copy in the memory. JVM only allocates static memory once. The memory allocation for static variables is completed during the process of loading the class. You can use the class name to directly access.

(5) Static code is bound to class. Successful loading of class means your static code has been executed and you will not do this again in the future. Static code. The function of Class.forName() is to ask the JVM to find and load the specified class, which means that the JVM will execute the static code segment of the class.

(6) Static method Attribute initialization is initialized when the class is loaded. Instead of static method property initialization.

Example:

public class Test {
     static int i;
     static
     {
          i++;
          System.out.println("outt i:"+i);
     }
     static void outi()
     {
            i++;
            System.out.println("output i:"+i);
     }
     public static void main(String [] argStrings)
     {
          Test test=new Test();
          test.outi();
     }
}

##Output result:

outt i:1
output i:2



##

The above is the detailed content of Detailed introduction to Java keyword static. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn