Java improvement--keyword static

Source: Internet
Author: User

First, what static represents

There is no concept of global variables in Java, but we can use static to implement a "pseudo-global" concept, where static in Java means "global" or "static," which is used to modify member variables and member methods, and of course to decorate blocks of code.

Java divides memory into stack memory and heap memory, where stack memory is used to hold some basic types of variables, arrays, and references to objects, and heap memory mainly stores some objects. When the JVM loads a class, if the class has static decorated member variables and member methods, it creates a fixed-size area of memory for these member variables and member methods at a fixed location, with these "fixed" features, so that the JVM can access them very easily. At the same time, if the static member variables and member methods do not appear scoped, their handles will remain unchanged. At the same time static concept implies that it is not recoverable, that is, in that place, you modify, he will not be back to the original, you clean up, he will not come back.

member variables and member methods that are also modified by static are independent of the class, and do not depend on a particular instance variable, that is, it is shared by all instances of the class. References to all instances point to the same place, and any modification to it will result in 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
Second, how to use static

Static can be used to modify member variables and member methods, which we refer to as static variables and static methods, accessed directly through the class name.

ClassName.PropertyName

Classname.methodname (...)

A static code block is a block of code that, when the JVM loads the class, executes the code, which is very useful. (for the use of code blocks these days, please pay attention to)

2.1. Static variables

Static modified variables, which we call statically variable, do not use static modified variables called instance variables, the difference between them is:

A static variable is initialized as the class loads, it has only one in memory, and the JVM allocates memory only once, and all instances of the class share static variables that can be accessed directly through the class name.

However, the instance variable is different, it is accompanied by the instance, each creation of an instance will produce an instance variable, which is die with the instance.

So we generally use static variables in both cases: sharing data between objects, convenient access.

2.2. Static method

The static modification method we call it a statically method, which we call directly through the class name. Since he exists at the time of class loading, it does not depend on any instance, so the static method must be implemented, that is, he cannot be an abstract method.

The static method is a special method in a class, and we declare the method static only when we really need them. All methods, such as the math class, are statically static.

2.3. Static code block

The static code block, called the static block of code, is executed with the load of the class, and can be placed at will wherever it is.

Third, the limitation of static

Static does have many effects, but it also has some drawbacks.

1, it can only call the static variable.

2, it can only call the static method.

3, can not be quoted in any form this, super.

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

Summary: Whether it is a variable, a method, or a block of code, as long as the static decoration, is when the class is loaded, it is "ready", that is, can be used or has been executed, can be executed from the object. Conversely, if there is no static, you must depend on the object instance.

Learn Java students pay attention to!!!
You are welcome to join the Java Learning Exchange Group when you encounter any problems in the learning process or want to acquire learning resources: 159610322 We'll learn java! together.

Java improvement--keyword static

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.