Java-keywords static and java keywords static

Source: Internet
Author: User
Tags java keywords

Java-keywords static and java keywords static
I. What does static mean?

The concept of global variables does not exist in Java, but we can implement a "pseudo global" concept through static. in Java, static represents the meaning of "global" or "static, it is used to modify member variables and member methods. Of course, it can also modify the code block.

Java divides memory into stack memory and heap memory. The stack memory is used to store some basic types of variables, arrays, and object references. The heap memory mainly stores some objects. When the JVM loads a class, if the class has static modified member variables and member methods, A fixed memory area is opened for these member variables and member methods at a fixed position. With these "fixed" features, the JVM can easily access them. At the same time, if static member variables and member methods do not have a scope, their handles will remain unchanged. At the same time, the concept of "static" contained in static indicates that it cannot be recovered. That is, if you modify it, it will not change back to its original state. You have cleared it, he won't be back.

The static modified member variables and member methods are independent of the class. They do not depend on a specific instance variable, that is, they are shared by all instances of the class. All instance references point to the same place. Any modification to an instance will lead to 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:2user2 userNumber:2
Ii. How to use static

Static can be used to modify member variables and member methods. We call it static variables and static methods for direct access through class names.

ClassName. propertyName

ClassName. methodName (......)

The Static modified code block indicates a Static code block. When the JVM loads a class, the code is executed, which is very large. (For details about how to use code blocks over the past few days, stay tuned)

2.1 static variables

Static modified variables are called static variables and instance variables without static modification. The differences between them are as follows:

Static variables are initialized when the class is loaded. There is only one static variable in the memory, and the JVM only allocates memory for it once. all instances of the class share static variables, you can directly access it by class name.

However, the instance variables are different. They are accompanied by an instance. Every time an instance is created, an instance variable is generated, which is the same as the instance.

Therefore, we generally use static variables in these two cases: sharing data between objects and convenient access.

2.2 static method

Static methods are called static methods. We call them directly through class names. Because it exists during class loading, it does not depend on any instance, so the static method must be implemented, that is, it cannot be an abstract method.

The Static method is a special method in the class. We can declare the method as static only when they are actually needed. For example, all methods of the Math class are static and static.

2.3 static code block

A static code block is called a static code block. A static code block is executed when the class is loaded and can be stored anywhere.

Iii. Static limitations

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

1. It can only call static variables.

2. It can only call the static method.

3. this and super cannot be referenced in any form.

4. static variables must be initialized before non-static variables.

Summary: whether it is a variable, method, or code block, as long as static modification is used, the class is "ready" when it is loaded, that is, it can be used or executed, can be executed without objects. Otherwise, it must depend on the object instance.

Notes for learning Java !!!
If you have any questions or want to obtain learning resources during the learning process, join the Java learning exchange group: 159610322 let's learn Java together!

Related Article

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.