Java keyword final, static usage summary

Source: Internet
Author: User

1. Static variables
There are two ways to classify a class member variable statically: A variable that is statically modified, called a static variable or a class variable, and another 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).
Static variables can only be initialized once
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 method
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 specific objects! This need to understand, want to understand the truth, not the memory!!!
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.

1 Final class
Final cannot inherit, that is, there is no subclass, the common string class is a final class, Java users are not allowed to extend the string class

2 Final method
If a method in the parent class is decorated with final, then this method does not allow subclasses to override

3 Final Constants
Because constants are not allowed to change during run time, constants do not have a default value when declaring.

What does static and final piece mean?
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.        One particular issue to note:for instance constants that are static and final modified, the instance itself can no longer be changed, but for instance variables of some container types (for example, ArrayList, HashMap), the container variable itself cannot be changed, but the object stored in the container can be modified. This is a lot of programming. Perhaps you have been confused by the way you have said so much, or by looking at an example: Public classteststaticfinal {
PrivateStaticFinalString Strstaticfinalvar ="AAA";
PrivateStaticString Strstaticvar =NULL;
Private FinalString Strfinalvar =NULL;
PrivateStaticFinalintIntstaticfinalvar = 0;
PrivateStaticFinalInteger Integerstaticfinalvar =NewInteger (8);
PrivateStaticFinalArraylist<string> Alstaticfinalvar =NewArraylist<string> ();

PrivatevoidTest () {
System.out.println ("----------\ r \ n Before-------------value is processed");
System.out.println ("Strstaticfinalvar="+ Strstaticfinalvar +"\ r \ n");
System.out.println ("Strstaticvar="+ Strstaticvar +"\ r \ n");
System.out.println ("Strfinalvar="+ Strfinalvar +"\ r \ n");
System.out.println ("Intstaticfinalvar="+ Intstaticfinalvar +"\ r \ n");
System.out.println ("Integerstaticfinalvar="+ Integerstaticfinalvar +"\ r \ n");
System.out.println ("Alstaticfinalvar="+ Alstaticfinalvar +"\ r \ n");


//strstaticfinalvar= "haha haha"; Error, final indicates an end state, and the variable itself cannot be changed.
Strstaticvar ="haha";//Correct, static represents a class variable, and the value can be changed.
//strfinalvar= "hehe hehe"; Error, final represents the final state, the initial value (even if given a null) at the time of definition, and cannot be changed once given.
//intstaticfinalvar=2; Error, final represents the final state, the initial value (even if given a null) at the time of definition, and cannot be changed once given.
//integerstaticfinalvar=new Integer (8); Error, final represents the final state, the initial value (even if given a null) at the time of definition, and cannot be changed once given.
Alstaticfinalvar.add ("AAA");//correct, the container variable itself has not changed, but the storage content has changed. This rule is very common and has many uses.
Alstaticfinalvar.add ("BBB");//correct, the container variable itself has not changed, but the storage content has changed. This rule is very common and has many uses.

System.out.println ("After-------------value is processed----------\ r \ n");
System.out.println ("Strstaticfinalvar="+ Strstaticfinalvar +"\ r \ n");
System.out.println ("Strstaticvar="+ Strstaticvar +"\ r \ n");
System.out.println ("Strfinalvar="+ Strfinalvar +"\ r \ n");
System.out.println ("Intstaticfinalvar="+ Intstaticfinalvar +"\ r \ n");
System.out.println ("Integerstaticfinalvar="+ Integerstaticfinalvar +"\ r \ n");
System.out.println ("Alstaticfinalvar="+ Alstaticfinalvar +"\ r \ n");
}

PublicStaticvoidMain (String args[]) {
NewTeststaticfinal (). Test ();
}
}The results of the operation are as follows:----------strstaticfinalvar=aaastrstaticvar=nullstrfinalvar=nullintstaticfinalvar= before-------------value processing 0integerstaticfinalvar=8alstaticfinalvar=[]----------After-------------value is processed strstaticfinalvar=aaastrstaticvar= haha haha strfinalvar=nullintstaticfinalvar=0integerstaticfinalvar=8alstaticfinalvar=[aaa, BBB]
Process finished with exit code 0looking at the above example, it's a lot clearer, but it's important to understand that the object "loaded" in a variable of a container type that is modified by static final modification can be changed. This is a very different place from the general basic type and the class type variable.

Java keyword final, static usage summary

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.