Static final keyword release in Java

Source: Internet
Author: User

Static
static means "global" or "static"
1. There are two types of class member variables that can be classified according to whether they are static: A variable that is statically modified, called a static variable or a class variable, and a variable that is not modified by static, called an instance variable. The difference between the two is:
For static variables there is only one copy in memory (memory saving), the JVM allocates only one memory at a time, completes the memory allocation of static variables during the loading of the class, can be accessed directly (conveniently) by the class name, and, of course, is accessible through objects (but this is not recommended).
For instance variables, each time an instance is created, the instance variable is allocated one memory, and the instance variable can have multiple copies in memory, with no effect (flexibility).
2. Static methods
Static methods can be called directly from the class name, and any instance can also be called, so a static method cannot use the This and Super keywords, and cannot directly access the instance variables and instance methods of the owning class (that is, member variables and member methods without static). Only static member variables and member methods of the owning class can be accessed. Because instance members are associated with a particular object.
Because the static method is independent of any instance, the static method must be implemented, not abstract.
3. Static code block
A static block of code is also called a code block, which is an independent block of static blocks in a class that is separate from a class member, can have more than one position, it is not in any method body, and the JVM executes these static blocks of code when the class is loaded, and if there are multiple static blocks of code, The JVM executes them sequentially in the order in which they appear in the class, and each code block is executed only once.

Final
Final has the meaning of "This is immutable" or "final state", which can be used to modify non-abstract classes, non-abstract class member methods, and variables.

    1. final类不能被继承,没有子类,final类中的方法默认是final的。    2. final方法不能被子类的方法覆盖,但可以被继承。    3. final成员变量表示常量,只能被赋值一次,赋值后值不再改变。    4. final不能用于修饰构造方法。

The final modified variable is intended to allow the method to not modify the contents/references of the variable (the base amount does not affect the original variable, the reference amount is changed to use the method)


Final can be defined without assigning values and increasing flexibility. The final variable in the class is assigned in the constructor.

static + final
Static final is used to modify member variables and member methods, which can be simply understood as "global constants"!
For variables, this means that once a value is given it cannot be modified and is accessible through the class name.
For methods, the representation is not overwritten and can be accessed directly through the class name.

    特别要注意一个问题:    对于被static和final修饰过的实例常量,实例本身不能再改变了。


However, for instance variables of some container types (for example, ArrayList, HASHMAP), you cannot change the container variable itself, but you can modify the objects stored in the container, which is a lot of programming.

publicclass Test{    publicstatic final Map<Integer, String> msgMap =         new HashMap<Integer, String>();    static{        "no permission to access!");        "not open to access!");        "access error");        "login error, please re-login!");        "system error, please try later!");    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Static final keyword release in Java

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.