Why can't this and super keywords be used in static members and static methods?

Why can't this and super keywords be used in static members and static methods?

1.      You cannot usethispredefined object reference in a,even if the operation behind it is also a static member.
Becausethisrepresents the reference to the object that calls this function,and static methods belong to classes,notbelongs to the object. After thestatic method is successfully loaded, theobject does not necessarily exist

 

2.      Before the question, let’s talk about the usage of super :
1.
The usage ofsimilar to this . This represents a reference to an object of this class, pointing to an object that has been created by this class; and super represents a reference to an object of the parent class, pointing to the object of the parent class ; 2. Static takes precedence over object existence; 3. Known from 1. and 2. abovebecause static takes precedence over object existence, after the method is statically modified, the method exists first, and theclass object pointed to by super is used in the method , but the required parent class reference object appears later than this method, that is , the object pointed to by super does not, of course, an error will occur. To sum up, the super keywordcannot appear in static methods



 

3.  First you have to understand the difference between objects and classes.
This
and super belong to the object category, while static methods belong to the class category.
All member methods have a default parameter this ( even a method without parameters ), as long as it is a member method , the compiler will Add this parameter to you, such as :

void method1(){} in
Class A is actually like this --------> void method1(A this)
void method2(int x){}
is actually like this The --------> void method2(A this, intx)
and the static method has nothing to do with the object , you can't pass the reference of the object to the method at all , so you can't use this

 

 

4.      If a method is defined asstatic, it is a static method, that is to say, this method can be called without an object of this class, and calling a static method is"class name.Method name"

Since"no need forobject can call the static method", and thethisandsuperkeywords are used for objects of this class-----Calling a static method does not require an object of this class. This sentence clearly shows thatthisandsuperkeywords

 

5.      The static method is stored in the data segment in the memory.This and super call the application object in the heap space and cannot call the data in the data segment area. Therefore, the thisandsuperkeywords cannot be usedinstaticmethods.

 

6.      Static methods and static classes do not belong to a single object, but are shared by all objects of the classandthisrepresents the current object

 

7.    Things only belong to classes, not to any objects, soTHISandSUPER.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325955868&siteId=291194637