Java keywords: This, super usage Summary

Source: Internet
Author: User
I. This The Java keyword "this" can only be used in methods and methods. After an object is created, the Java Virtual Machine (JVM) assigns a pointer to the object to reference itself. The pointer name is this. Therefore, this can only be used in non-static methods in the class, static methods and staticCodeThis cannot appear in the block, which is clearly explained in the article "Java keyword static, final usage summary. In addition, this is only associated with a specific object, but not the class. Different objects of the same class have different this. Here is a comprehensive example of using this to illustrate the problem:
Package Org. leizhimin; public class test6 {private int number; private string username; private string password; private int x = 100; Public test6 (int n) {number = N; // This can also be written as: This. number = N;} public test6 (int I, string username, string password) {// The member variable has the same name as the parameter. The member variable is blocked and "this. to access member variables. this. username = username; this. password = password;} // The default constructor without parameters public test6 () {This (0, "unknown", "null "); // call another constructor through this} public test6 (string name) {This (1, name, "null "); // call another constructor through this} public static void main (string ARGs []) {test6 T1 = new test6 (); test6 t2 = new test6 ("Tourist "); t1.outinfo (T1); t2.outinfo (T2);} private void outinfo (test6 t) {system. out. println ("-----------"); system. out. println (T. number); system. out. println (T. username); system. out. println (T. password); F (); // This can be written as: This. F ();} private void F () {// The local variable has the same name as the member variable. The member variable is blocked and "this. to access member variables. int X; X = This. X ++; system. out. println (x); system. out. println (this. x) ;}// return the reference of the current instance private test6 getself () {return this ;}}

 

The running result is as follows: -----------
0
Unknown
Null
100
101
-----------
0
Visitors
Null
100
101
Let's look at the example above to explain under what circumstances the use of this:
First, call another constructor using this (parameter list). This is only used in the constructor of the class and cannot be used in other places.
2. When the name of a function parameter or local variable in the function is the same as that of a member variable, the member variable is blocked. To access the member variable, you must use "this. to reference member variables. Of course, without the same name, you can directly use the name of the member variable instead of this. If you use this name, it is no error.
Third, when the function needs to reference the current object of the class to which the function belongs, use this directly. In fact, these usage summaries come from a deeper understanding of the phrase "this is a pointer to the object itself". It is easy to forget and make mistakes, so you must understand it!
Ii. Super The key of super is similar to this. It is a blocked member variable or member method or becomes visible, or used to reference the blocked member variable and member method. However, super is used in subclass to access the blocked members in the direct parent class. Note that it is a direct parent class (that is, the nearest superclass above the class ). The following is an example of the comprehensive use of super. There are two classes: one father class and one father class sub-class son. These two classes fully demonstrate the usage of super. Here is the code:
Package org. leizhimin; public class father {Public String v = "father"; Public String x = "output the public member variable X of the father class !!! "; Public father () {system. Out. println (" Father constructor is called! ");} Public father (string v) {This. V =" parameter constructor of the father class! Running. ";} public void outinfo () {system. out. println ("Father outinfo method called");} public static void main (string [] ARGs) {// todo automatically generate method stubs} package Org. leizhimin; public class son extends father {Public String v = "son"; Public son () {super (); // call the superclass constructor, which can only be placed in the first row. system. out. println ("the son construction method without parameters is called! "); // Super (); // The error must be placed at the top of the constructor body .} public son (string Str) {super (STR); system. out. println ("son has a parameter constructor called! ");} // Overwrite the superclass member method outinfo () Public void outinfo () {system. out. println ("Son outinfo () method called");} public void test () {string v = "Hahahaha! "; // The local variable v overwrites the member variable V and the super variable V system. out. println ("------ 1 -----"); system. out. println (V); // output the local variable V system. out. println (this. v); // output (subclass) member variable V system. out. println (super. v); // output the super class member variable V system. out. println ("------ 2 -----"); system. out. println (x); // output the super class member variable v. The subclass inherits the system. out. println (super. x); // output the super class member variable V system. out. println ("------ 3 -----"); outinfo (); // call the outinfo () method of the subclass this. outinfo (); // call the outinfo () method super of the subclass. outinfo (); // call the outinfo () method of the parent class} public static void main (string [] ARGs) {New son (). test ();}}

 

Sub-class son running result: The father constructor is called!
Son's non-parameter constructor is called!
------ 1 -----
Hahahaha!
Son
Father
------ 2 -----
Output The Public member variable X of the father class !!!
Output The Public member variable X of the father class !!!
------ 3 -----
Son's outinfo () method is called
Son's outinfo () method is called
Father outinfo method called
Note: In this example, only the usage of super is described. In actual design, the class is generally private as much as possible. Through the example above, the usage of super is summarized below:
1. To call the constructor of the parent class in the subclass constructor, use the "super (parameter list)" method to call the constructor. The parameter is not required. Note that the "super (parameter list)" statement can only be used in the first row of the subclass construction method body.
2. When the local variable in the subclass method or the member variable of the subclass has the same name as the parent member variable, that is, when the subclass local variable overwrites the parent member variable, use "super. the member variable name to reference the parent class member variable. Of course, if the parent class member variables are not overwritten, you can also use "Super. member variable name" to reference the parent class member variables, but this is unnecessary.
Third, when the member methods of the subclass override the member methods of the parent class, that is, the subclass and the parent class have the same method definition (but the method body can be different). At this time, use "super. method (Parameter List) to access the parent class. The usage of this and super is nothing more than this. Only by understanding the principles of this will we not fall into the trap!
from: http://lavasoft.blog.51cto.com/62575/18886

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.