Java's super and this keyword usage summary

Source: Internet
Author: User
Tags instance method

------Super keyword------

Super Purpose: Access superclass "hidden member variables (whether static or not) and static methods" and "overridden instance methods" in subclasses. The superclass here must be a "direct superclass", the most recent superclass above the subclass.
How to use Super:
① calls the constructor of the superclass in the subclass construction method, called with "super (Paramlist)", and paramlist can be empty depending on how the superclass is constructed. Also super (paramlist) must be the first sentence of the subclass construction method.
② when a member variable of a superclass is hidden because it has the same name as a member variable of a subclass (the member variable is hidden from static), you can use "super.membervariablename" to access the member variables of the superclass.
Note: Some people say that when a member variable of a superclass has the same name as a local variable or parameter of a method of a subclass, it is also hidden. So once you leave this method of hiding, it is clear that this is wrong. The hidden concept is limited to the range of member variables, and the local variables in the method body belong to the same name as the member variables below this is what to discuss.
Assume that there is a superclass parent, where there is a member variable A, subclass child, where there are method methods (type A) present:
1) If the subclass does not hide the superclass member variable A, in method (Type a), regardless of the use of this. A or super.a are the same, access the superclass member variable A; A is inherited, in other words, a member variable that is inherited by nature (no hidden/rewritten), or even a method, Super.membername=this.membername)
2) If the subclass has member variable A that hides the superclass member variable A, in method (Type a), super. A=parent. A,this. A=child. A, there is a difference between the two.
③ when a static method/instance method of a superclass is hidden/overridden, you can use "Super.methodname (paramlist)" To access the superclass method. For static methods, this is no surprise, and for instance methods, this usage makes the superclass overridden methods visible in subclasses.

Finally, be aware that the Super keyword cannot be used in static methods! ------The This keyword------
Purpose of this: Refers to the object itself.
When an object is created, the Java Virtual machine assigns a pointer to the object that refers to itself, which is the name of this pointer. Therefore, this can only be used in non-static methods in the class, and the static method and the static code block must never appear in this. And this is only associated with a particular object, not with a class, and different objects of the same class have different this.
This usage:
① in the constructor method, use this (paramlist) to call its own other construction method, this (paramlist) must be placed in the first sentence position! This usage is limited to use in construction methods.
② the parameter or local variable of a method has the same name as a member variable, it needs to be "this.membervariablename" in order to access the member variable in the method.
③ A method needs to refer to the current object of its owning class, use this to refer to the current object directly.
Example code:
[Java]View Plaincopy
  1. Import Java.lang.reflect.Field;
  2. Import Java.lang.reflect.Modifier;
  3. Class A {
  4. int x;
  5. static double y;
  6. Char Z;
  7. A (char z) {
  8. this.z = z; //This accesses the member variable to differentiate the parameter with the same name
  9. }
  10. A (double newy, char newz) {
  11. double y = newy; //This accesses member variables to distinguish local variables with the same name
  12. char z = newz;
  13. this.y = y; //Because Y is a static member variable, it is best to use Classname.staticmembervarname or a.y to access
  14. this.z = z;
  15. }
  16. A (int A, double b, char c) {
  17. This (b, c); //Use this (paramlist) call another constructor method must be placed in the first sentence position
  18. this.x = A; This (x) can no longer be used here;
  19. }
  20. void OutPut () {
  21. if (this.equals (this))//This is used as an object reference, of course this condition is always =true
  22. System.out.println ("I am Class" + getclassname () + "method"
  23. + Getinvokemethodname ());
  24. }
  25. String GetClassName () {
  26. return This.getclass (). GetName ();
  27. }
  28. String Getinvokemethodname () {
  29. String tempname = new Exception (). Getstacktrace () [1].getmethodname ();
  30. return tempname;
  31. }
  32. static void Showstaticfieldvalue (Object obj) throws Exception { //
  33. Field fields[] = Obj.getclass (). Getdeclaredfields ();
  34. String FieldName, FieldModifier, FieldType;
  35. Object Val;
  36. For (int i = 0; i < fields.length; i++) {
  37. Field field = Fields[i];
  38. if (field.tostring (). IndexOf ("static")! =-1) {
  39. //System.out.println (field.tostring ());
  40. FieldName = Field.getname ();
  41. FieldType = Field.gettype (). GetName ();
  42. FieldModifier = modifier.tostring (Field.getmodifiers ());
  43. Field.setaccessible (true);
  44. val = field.get (obj);
  45. System.out.println (FieldModifier + " " + FieldType + ""
  46. + FieldName + "=" + val);
  47. }
  48. }
  49. }
  50. void GetStaticFieldValue () {
  51. try {
  52. Showstaticfieldvalue (this);
  53. } catch (Exception e) {
  54. }
  55. }
  56. }
  57. Class B extends A {
  58. static double x; To hide the member variable x for Super Class A
  59. int y = (int) this.x + 1;   Hide the member variable y of super Class A, some say this must be used in the method body, mercilessly break its rumors.
  60. //inherits the member variable Z of Super Class A, still char Z;
  61. B (char c) {
  62. super (c); /* 
  63. * SUPER (C) The purpose of calling the superclass constructor is to initialize the superclass member of the natural inheritance, and if the subclass completely hides the member variable of the superclass, it can be used without super
  64. * SUPER (Paramlist) Access Superclass constructor question: 1. Class B hides and changes the member variable of Class A.
  65. * SUPER (b) is the value of the A object contained in B changed only, and the value of static double X in B is still the default value? (Pending verification)
  66. * 2.java Construction method is an instance method or a static method?
  67. */
  68. x = Super.y + 1.0; Super Access Super class is hidden member variable double y, can also be written as A.y
  69. y = super.x; Super Access Super class hidden member variable int x
  70. }
  71. String GetClassName () {//rewrite getclassname of superclass A ()
  72. return "Class-class BBB";
  73. }
  74. String Getinvokemethodname () {//rewrite getinvokemethodname of superclass A ()
  75. Return "Method Name Method name Method name ";
  76. }
  77. void Output () { //overrides the output instance method of the superclass.
  78. System.out.println ("Super Call Super Class overridden two method output: Class name ="
  79. + Super. GetClassName () + ", Method name =" + super. Getinvokemethodname ()); //Super Call instance method of superclass
  80. System.out.println ("Output with the overridden method of Class B: class name =:" + getclassname () + ","
  81. + ", Method name =" +getinvokemethodname ()); //Call your own method
  82. }
  83. static void Showstaticfieldvalue (Object obj) throws Exception { //Hide Superclass method
  84. System.out.println ("Static Field doulbe x=" + x);
  85. }
  86. void GetStaticFieldValue () {
  87. try {
  88. System.out.println ("Super calls the method of super Class A to output static member variable");
  89. Super.showstaticfieldvalue (this); //Super Call the static method that the superclass is hidden
  90. System.out.println ("Class B's own method outputs static member variables");
  91. Showstaticfieldvalue (this); Class B's Own method
  92. } catch (Exception e) {
  93. }
  94. }
  95. }
  96. Class Example3_15 {
  97. public static void Main (string[] args) {
  98. A MyA = new A (8, 6.0, ' K ');
  99. b MyB = new B (' Oh ');
  100. Mya.getstaticfieldvalue ();
  101. Mya.output ();
  102. System.out.println ("====================");
  103. Myb.getstaticfieldvalue ();
  104. Myb.output ();
  105. }
  106. }

Java's super and this keyword usage summary

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.