final+static

final

As the name suggests the final keyword for "final", meaning that can not be changed. Its definition, can be broadly divided into the following three points:

  1. The modified final class can not be inherited;
  2. The final modified method can not be rewritten;
  3. The modified final variables can not be changed. Note: reference type variable, a reference is not changed, but the contents of the reference points is variable

If a method is modified final, then at compile time, JVM will seek its inline, which is equivalent to the method statements are added to the code calls the method, the removal of this method. Call a function that requires a certain overhead: to protect the scene, the establishment of the stack, such as site restoration, for some small amount of code, but also frequently called functions, the consumption of time and space are great, so inline help enhance the efficiency of Java. So if you have to determine whether a method is not inherited, to try to modify a final.

static

The static keyword can also be used to modify the classes, methods and variables, but it can only be modified class inner class, it modifies the methods and variables can be called "static method", the "static variable" is also known as " class method "," class variables ", the latter title is clearly closer to its actual meaning. As opposed to "class method", "class variable" is "instance method", "instance variables" that is not modified by the keyword static methods and variables. A class can have many objects (instances), non-static variables for each object has, and static variables, it is the total of all objects.

  • Static resources are independent of the type exist, all instances share the load when the class initialization;
  • In new non-static resources is a new object when it is loaded, generate an instance of the time.

Through the above two points can be drawn:
when the class is initialized only, time has not yet produced instance, non-static resources is not loaded out, and therefore reference non-static resources is wrong in a static method
when the parent class subclass there are static and non-static resources resources when the first child class object when the new order is the redeployment of resources: the parent class static resources -> subclass of static resources -> parent class constructor -> subclass constructor. Because the static resource initialization only once, when the new second child class object, call the resources of the order is the parent class constructor -> subclass constructor

Guess you like

Origin www.cnblogs.com/sy130908/p/11429521.html