Detailed final and static

We mentioned in a blog post on the fianl keyword for this keyword, they also spent a beginner in the effort dramatically, and even the brothers had a heated discussion, and, in our discussions, and try to read a variety of information, and ultimately come to the right, easy to understand explanation.

Now, I introduce final keyword:

final :

First of all, I have to answer now the hearts of the students may have doubts - Why it final?

Because inheritance phenomenon (the point in my knowledge of a cover (overridden method) of "covering Detailed methods - toString () and equals () covered" ) explain in more detail, and sometimes we do not want subclasses to "cover" the parent class of java this gave us a key to this case:. final

Well, now I use this keyword to introduce modified characteristics of it:

final modification characteristics:
Modified class : the modified class can not be inherited
modification method : modified method can not be overridden
modified variables :

  1. Only the initial value at compile time , and after the unchanging constants ;
  2. In defining such constants when you have to assign it a value . Because fianl types of variables, and will not be assigned at the time of the initial construction method, so when we define final type of variable manually to its initial value, otherwise it will not compile ;
  3. A reference to the object, fianl keyword makes constant references . Once the reference is initialized to point to an object, you can not then change it to point to another object.
    However, the object itself can be modified , JAVA does not provide any way to make the object constant.
    (This restriction also applies arrays, because arrays are objects)

For the "reference" concept can be understood as an object "pointer", but is vary, the students do not worry, use and Bowen will explain this knowledge after himself.

And we are in the stage of learning SE are generally used to modify the final variable, when we use the final keyword to modify a variable, it's functionally equivalent to us in the C language in the " macro ", the main purpose is to avoid "immortal code appears "the code logic clear

Well, now, I have to tell us about the static keyword it.

static:

About this keyword, we are not strange, because here it's name as in the C language, is called a static member with the members of the static keyword repair.

Now, I have to introduce static keyword features :

  1. With loads and loads of class :
    static members in the JVM class loader , it was applied for space. That is to say, in a former class has not been instantiated object , it has been a good application of the space static members;
  2. All objects are shared classes :
    static members are not "private property", with a static member is in the same space all the objects of this class;
  3. Static methods can not reference non-static members and non-static methods .
    However, non-static method can be called static members and static methods;
  4. It can be by calling the class name :
    static and static methods are generally invoked as follows:
    (1) static member class name;.
    (2) static method class name ();.
  5. Allowed this static method :
    This is because:
    a static class and is loaded with the load, this is with the creation of the object exists.
    Static object exists than before. Therefore, we can not appear this in a static method

Well, there is static keyword modified variable feature is at an end, and now I am speaking off his local variables , class of non-static members and static member class between the difference of it:

Static variables and member variables difference:

  1. Different memory location :
    Static variables are stored in the method area static zone
    member variables stored in the heap memory
  2. Belongs different :
    static variables belong to the class , it is also known as class variables
    member variable belongs to the object , it is also known instance variable (object variable)
  3. Memory appear at different times :
    the static variable with the kind of loaded and loaded, and disappear with the disappearance of the class
    member variables with the creation of the object exists, the object disappear with the disappearance
  4. Call different :
    Static variables may be by class name calling , can also be invoked by an object , it is recommended to use the class name to call
    member variables can only be invoked by object name

In the final Finally, summarize:

  • final key is to ensure that the space is unchangeable , and only within the class method is invoked;
  • static keyword to ensure that the space value assignment well in advance , and can be in this project use anywhere

Guess you like

Origin www.cnblogs.com/codderYouzg/p/12416415.html