Static, interface and final, staticinterface

Source: Internet
Author: User

Static, interface and final, staticinterface

1. static:

A) abstract class: the class modified by the abstract keyword is called an abstract class. Abstract classes cannot be instantiated, and an object (Instance) of an abstract class cannot be created ).

Abstract method: The method modified by the abstract keyword is called an abstract method. Abstract methods must be defined in abstract classes. Compared with abstract methods, the methods previously defined are called specific methods (with declarations and implementations ). Abstract METHODS can only exist in abstract classes and interfaces, but abstract classes can contain specific methods. interfaces can only contain abstract methods.

B) Inheritance of abstract classes: When the subclass inherits the parent class (the parent class is an abstract class), The subclass must implement all the abstract methods defined in the parent class; otherwise, this subclass must be declared as an abstract class.

C) static modification attribute: no matter how many objects a class generates, all these objects use a unique static member variable. An object modifies the static member variable, the value of this static member variable of other objects also changes. If a member variable is static, we can use it by class name and member variable name (this method is recommended ).

D) static modification method: static modification method is called static method. For static methods, you can access them by class name and method name. Static methods can only be inherited and cannot be overwritten ).

 1 package eameple; 2 public class Test 3 { 4     public static void main(String[] args) 5     { 6         R r = new R(); 7         r.method(); 8         T t= new R(); 9         t.method();10     }11 }12 13 abstract class T14 {15     16     public static void method()17     {18         System.out.println("test");19     }20 }21 class R extends T22 {23     public static void method()24     {25         System.out.println("method");26     }    27 }

E) static code block: static code block. Static code blocks also perform initialization. First, execute the static code block, and then execute the constructor. Static code blocks are executed when classes are loaded, and the constructor is executed when objects are generated. To call a class to generate objects, first, you need to load the class to the Java Virtual Machine (JVM), and then the JVM loads the class to generate the object. The static code block of a class is executed only once. It is executed when the class is loaded. Because each class is only loaded once, the static code block is executed only once; the constructor method does not call the constructor of the class every time an object is generated. Therefore, the constructor is called once when a new object is created. If there are both constructor methods and static code blocks in the inheritance system, first execute the static code block of the top-level class and continue to execute the static code block of the bottom-level class, then, execute the constructor of the top-level classes until the constructor of the bottom-level classes are executed. Note: The static code block is executed only once.

F) Static users can only access static objects. Non-static users can access everything.

G) The this keyword cannot be used in static methods.

2. interface

Interface: an interface is equivalent to a class. All methods in an interface are abstract methods. When declaring methods in an interface, you can use the abstract keyword or not. In general, abstract keywords are omitted. Class can implement interfaces, which are represented by the keyword implements, indicating that a class implements an interface. If a class implements an interface, the class must implement all the methods declared in the interface. If this class is an abstract class, you do not need to implement methods in the interface. Java is a single inheritance, that is, a class can only have one unique parent class. A class can implement multiple interfaces, and multiple interfaces are separated by commas. All methods declared in the interface are abstract methods. All methods in the interface are public. You can also define member variables in the interface. All the member variables in the interface are public, final, and static.

3. final

Final keywords: final can modify attributes, methods, and classes. When a class is modified by final, it indicates that the class is a final class, that is, it cannot be inherited. When a method is modified by final, it indicates that the method is a final state method, that is, it cannot be overwritten ). When an attribute is modified by final, it indicates that the attribute cannot be rewritten. When final modifies a native data type, it indicates that the value of the native data type cannot be changed (for example, it cannot be changed from 10 to 20). If final modifies a reference type, it indicates that the reference type can no longer point to other objects, but the content of the object pointed to by the reference can change.

For final type member variables, there are generally two initial value assignment methods: Assign the initial value when declaring final type member variables; Do not assign the initial value when declaring final type member variables, however, the initial value is assigned to all constructor methods of the class.

 

A class cannot be both final and abstract. The main purpose of abstract is to define a convention for sub-classes to implement this Convention, while final indicates that the class cannot be inherited, in this case, abstract expects that the class can be inherited, while final explicitly states that the class cannot be inherited. The two are in conflict. Therefore, a class cannot be both final and abstract.

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.