Home Java javaTutorial Detailed introduction to Java keyword static

Detailed introduction to Java keyword static

Mar 16, 2017 am 09:52 AM

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();
     }
}
Copy after login

##Output result:

outt i:1
output i:2
Copy after login



##

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles