search
HomeJavajavaTutorialJava improvement (6) -----Keyword static

1. What does static represent?

There is no concept of global variables in Java, but we can implement a "pseudo-global" concept through static. In Java, static means "global" or "Static" means, used to modify member variables and member methods, of course, it can also modify code blocks.

Java divides memory into stack memory and heap memory. Stack memory is used to store some basic types of variables, arrays and object references, and heap memory mainly stores some objects. When the JVM loads a class, if the class has statically modified member variables and member methods, a fixed-size memory area will be opened for these member variables and member methods at a fixed location. With these "fixed" features , then the JVM can access them very conveniently. At the same time, if static member variables and member methods do not go out of scope, their handles will remain unchanged. At the same time, the concept of "static" contained in static means that it is irrecoverable, that is, if you modify it at that place, it will not change back to its original state. If you clean it up, it will not come back.

                                                                                                                                                                      The member variables and member methods being statically modified are independent of the class. It does not depend on a specific instance variable, which means that it is shared by all instances of the class. The references of all instances point to the same place, and modifications to any one instance will cause changes to other instances.

public class User {  
    private static int userNumber  = 0 ;  
      
    public User(){  
        userNumber ++;  
    }  
      
    public static void main(String[] args) {  
        User user1 = new User();  
        User user2 = new User();  
          
        System.out.println("user1 userNumber:" + User.userNumber);  
        System.out.println("user2 userNumber:" + User.userNumber);  
    }  
}      
------------  
Output:  
user1 userNumber:2  
user2 userNumber:2

2. How to use static

Static can be used to modify member variables and member methods. We call them static variables and static methods, which can be accessed directly through the class name.

        ClassName..propertyName

            ClassName.methodName(……)

##                                                                                                                                                                                                           Block code is very useful. (For the introduction of the use of code blocks in the past few days, please pay attention)

2.1. Static variables

Variables modified by static are called static variables, and variables that are not modified with static are called variables. are instance variables. The difference between them is:

The static variable is initialized when the class is loaded. It has only one in the memory, and the JVM will only allocate memory for it once. At the same time All instances of a class share static variables, which can be accessed directly through the class name.

But the instance variable is different. It is accompanied by the instance. Every time an instance is created, an instance variable is generated, which lives and dies with the instance.

So we generally use static variables in these two situations: data sharing between objects and easy access.

2.2. Static method

The static modified method is called a static method, and we call it directly through the class name. Since it exists when the class is loaded, it does not depend on any instance, so the static method must be implemented, which means that it cannot be an abstract method.

Static method is a special method in a class. We only declare methods as static when we really need them. For example, all methods of the Math class are static.

2.3. Static code block

The code block modified by static is called a static code block. The static code block will be executed as the class is loaded, and it can be placed at will. Can exist anywhere.


3. Limitations of Static

Static does have many functions, but it also has some flaws.

1. It can only call static variables.

2. It can only call static methods.

3. This and super cannot be quoted in any form.

4. Static variables must be initialized when they are defined, and the initialization time must be earlier than non-static variables.

Summary: Whether it is a variable, method, or code block, as long as it is modified with static, it will be "ready" when the class is loaded, that is, it can be used or has been executed, and can be detached. object and executed. On the contrary, if there is no static, it must depend on the object instance.

The above is the content of Java Improvement Chapter (6) ----- keyword static. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!