Learning the static and final Keywords of Java, javastaticfinal

Source: Internet
Author: User

Learning the static and final Keywords of Java, javastaticfinal

Static: (static modifier) the content of static modifier in Object-Oriented objects belongs to classes rather than objects. Therefore, static modifier member variables are generally called class member variables, static modification is generally called a class method.

Category:

1. static variables, also known as static variables or class variables. The other is a variable that is not modified by static, called an instance variable.

2. static methods are also called static methods or class methods. static variables and instance methods cannot be defined in static methods.

3. static code block. In a static block, you can access static variables and call static methods.

Note:

1. If static is not attached to any object, there is no this method.

2. static methods cannot call non-static methods, but non-static methods can call static methods.

3. Methods or variables modified by static do not need to be accessed by objects. As long as the class is loaded, the class name can be used for access.

4. The static method cannot be overwritten. When the subclass and the parent class have the same static method, the static method of the parent class is called by default, the static method of the subclass is hidden.

5. static blocks are generally used to initialize static variables in the class. Based on the Content of static modification, follow the principle of first defining first execution.

6. If variables or methods are often called, use static modification. Otherwise, use less to avoid Memory leakage.

Static variable code example:

Public class Test {static int a = 1; // static variable int B = 2; // instance variable public static void main (String [] args) {System. out. println (Test. a); // System. out. print (a); System. out. println (B); // The Test t = new Test () error will be reported when the variable B is directly output; // create the Instance Object System. out. println (t. b); // object call variable }}

Static Method code example:

Public class Test {static int a = 1; // static variable int B = 2; // instance variable static void A () {// static method B (); // an error is returned. The static method cannot call the non-static method System. out. println (Test. a); // System. out. print (a);} void B () {// non-static method Test. A (); // A (); A non-static method can call the static method System. out. println (B); System. out. println (Test. a); // System. out. print (a);} public static void main (String [] args) {Test. A (); // A (); B (); // an error is reported. Non-static methods must call Test t = new Test () through the instance object. t. B () ;}}/** can be overwritten? */class StaticSon extends Test {// subclass inherits the parent class void A () {// an error is reported, the static method of the parent class cannot be overwritten /*...... */} static void A () {// This is the static method of the subclass. Strictly speaking, it is not the static method of rewriting the parent class }}

Static block code example:

Public class Test {int c = 3; static int a = 1; static int B = 2; static {a = 10; B = 20; c = 30; // an error is reported, the variable in the static block must be the static variable System. out. println (a + B);} public static void main (String [] args) {Test t = new Test (); System. out. println (t. c); // execute static variables first, and then execute instance variables }}

Final: It is considered immutable.

Note:

1. final and static are often used together.

2. The final variable can be assigned only once.

3. The final method cannot be overwritten, but can be overloaded.

4. The final class cannot be inherited.

5. The two keywords final and abstract are opposite, and classes cannot be modified at the same time. Because final cannot be rewritten, abstract must be rewritten.

6. The final keyword is different from the finally keyword, which is used for exception handling.

7. In most cases, final is not used to modify methods and classes because of poor scalability.

8. Using final in a certain environment can improve the running performance of the program and optimize the program structure.

Example code of final variable and final class:

Public final class Test {final static int a = 1; static {a = 10; // an error is returned. Only one value assignment operation can be performed.} class FinalSon extends Test {// an error is returned, final class cannot be inherited}

Final method code example:

Public class Test {final static int a = 1; final void A () {// final method System. out. println (a) ;}} class FinalTest extends Test {void A () {// error. The final method cannot be overwritten. out. println ("err") ;}} final abstract class FinalErr {// both final and abstract cannot exist at the same time /*...... */}

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.