Two special variables in Java: this and super

Source: Internet
Author: User

 

InJava has two very special variables: this and super, which do not need to be declared before use. This variable is used inside a member function and points to the current object. The current object refers to the object that calls the method currently being executed. A super variable is a constructor that directly points to a super class and is used to reference variables and methods in a super class. Therefore, they are all very useful variables. Next I would like to introduce how to use this and super.

1. this

Let's take a look at a piece of code:

Class PersonInformation

{

String name, gender, nationality, address;

Int age;

Void PersonInformation (String p_name, String p_gender, String p_nationality, String p_address, int p_age)

{

Name = p_name;

Gender = p_gender;

Nationality = p_nationality;

Address = p_address;

Age = p_age;

}

}
 
You will find that the method of this object in the PersonInformation () function prompts that you can directly access the member variables of the object, and in the same range, defining two local variables with the same name is not allowed, if you really want to make the member variables of the class and the parameters or methods of the local variables defined by yourself have the same name, you need to think of a method to make the member variables have the same name as the method parameters or local variables. differentiate, this variable must be used. Next I want to rewrite the code above so that every parameter of the PersonInformation constructor has the same name as the object member variable, and the initial value of the member variable is given by the parameter.

Class PersonInformation

{

String name, gender, nationality, address;

Int age;

Void PersonInformation (String name, String gender, String nationality, String address, int age)

{

This. name = name;

This. gender = gender;

This. nationality = nationality;

This. address = address;

This. age = age;

}

}

In the previous example, we can see that this must be used in the constructor, And this is used to point to the object instance that references the method currently being executed, the type of this variable is always a class that contains the pre-execution method. In the above example, we need to distinguish the parameter name from the member variable name. Writing name = name is obviously not allowed, when a parameter or local variable has the same name as a class member variable, the parameter or local variable has a higher priority. In this way, the parameter name or local variable name in the method body hides the member variable with the same name, therefore, to specify the value name member variable, you must use this to explicitly specify the current object.

In some cases, we can fully access the current object instead of accessing a specific instance object. We can also use this and use toString () in Java () (it can return a string describing this object) if any object is passed to System. out. in the println method, this method calls the toString method of this object and prints the result string. Therefore, we can use the following method: System. out. println (this) to print the current status of any inherent parameters of the method.

Another usage of this is the first statement of the constructor, which is in the form of this (parameter table). this constructor will call another constructor of the same class. See the following example:

Class UserInfo

{

Public UserInfo (String name)

{

This (name, aNewSerialNumber );

}

Public Userinfo (String name, int number)

{

UserName = name;

UserNumber = number;

}

}

If you call UserInfor newinfotable = new UserInfo ("Wayne Zheng"), the UserInfo (String name, int number) constructor is automatically called.

It can be seen that mastering this is very important in the Java programming process.
2. super

In Java, sometimes the member variables or methods in the subclass are the same as the member variables or methods in the super class (also known as the parent class, because the member variables or method names in the subclass have a high priority, the member variables and methods with the same name in the subclass hide the member variables or methods of the superclass, however, if we want to use this member variable or method in the superclass, super is required. Please refer to the following class.

Class Country

{

String name;

Void value ()

{

Name = "China ";

}

}

In the subclass below, the member variables and methods of the Self-class hide the member variables name and method value () of the super class (),

Class City extends Country

String name;

Void value ()

{

Name = "Hefei ";

Super. value ();

System. out. println (name );

System. out. println (super. name );

}
 
In order to reference the member variables name and method value () in the super class in the subclass, we use super, super. name and super. value () in the Code ();

Therefore, the displayed result is

Hefei

China

If we want to use a super-class constructor, we should use the super (parameter list) form.

 

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.