Static and final keywords

Source: Internet
Author: User

Static keywords:

It can be used to modify attributes in a class, methods in the class, and a specific class.

1. Used to modify attributes:

It indicates that this attribute belongs to the entire class. No matter how many object instances there are, all instances share a static member variable. This variable belongs to the entire class, rather than a specific instance. You can directly access the corresponding member variables through the class name.

2. modification method:

Static methods are called static methods. The modified static methods can be called directly through an instance, or directly by class names.

Static Method inheritance:

Static methods can be inherited, but the static method subclass cannot override the static method of the parent class. The static method of the subclass or the static method of the parent class depends on the referenced type. If the static method of the subclass is referenced, the static method of the subclass is called, it is a reference of the parent class and calls the static method of the parent class.

Note that you cannot access non-static member variables in static methods., Using the reverse identification method, assuming that all access is allowed, for example, modifying the value of an attribute in a static method. For static method access, the class name is used. static Method Name. If a non-static attribute is modified in this method, which of the many instances of this class has been modified or not? This is not clear. Therefore, static methods cannot access non-static member variables. Static methods can only access static member variables. Non-static methods can access all types of member variables.

Note that this cannot be used in the static method.Because this indicates the reference of the current object, even if it must be an object, but when the static method is accessed through the variable name, it is impossible to determine which object is called, because static storage belongs to the entire class, this cannot be used in static methods, and an error is returned.

There is an inheritance relationship between the parent class and the Child class (extends). It can only be static or non-static. In other cases, an error is reported.

Final keywords:

It can be used to modify classes, methods, or attributes.

1. When a class is modified by final, the final state of the BIOS class indicates that this class cannot be inherited.

2. When a method is modified by final, it indicates that the method is a final state method. This method cannot be overwritten. Otherwise, an error is returned.

3. When an attribute is modified by final, it indicates that the attribute cannot be rewritten. Note that the final attribute must be assigned an initial value. If the corresponding attribute is a reference, it indicates that the reference cannot be rewritten, indicating that the reference cannot point to other objects. This reference can only point to the object declared at the beginning, you cannot point to a new object, but the object value can be changed. Pay attention to the difference here.

The final attribute must be assigned a value during initialization. One is to assign a value directly when declaring a variable, or assign a value to the final attribute in the constructor of this class.

Static code block

The static code block is executed before the constructor. when the class file is loaded to the Java virtual machine, the static code block is executed, and the constructor written by the class is executed only when the object is generated.

In the following example, class B and class C inherit Class A respectively, but the static code block of Class A is only executed once, because Class A is only loaded once.

package statictest;class A{static{System.out.println("this is class A");}public A(){System.out.println("this is the constructor of class A");}}class B extends A{static{System.out.println("this is class B");}public B(){System.out.println("this is the constructor of class B");}}class C extends A{static{System.out.println("this is class C");}public C(){System.out.println("this is the constructor of class C");}}public class test {public static void main(String[] args) {B b1=new B();C c=new C();B b2=new B();}}

  

Output:

This is Class

This is Class B

This is the constructor of Class

This is the constructor of Class B

This is class C

This is the constructor of Class

This is the constructor of class C

This is the constructor of Class

This is the constructor of Class B

The results show that the above program will execute the static blocks of Class A, Class B, and class C at a time. The execution sequence is from parent class to subclass class. It can also be seen from the running results that the static code block is. the class file is automatically executed when it is loaded, and the constructor is called when a new object is generated, the sequence is to execute the constructor of the parent class before executing the constructor of the Child class. Two instance objects of B are generated later, but the static code block is not executed again. It can be seen that the static code block of each class is only loaded once, constructor can be executed multiple times.

Static and final keywords

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.