8.Java this keyword

Source: Internet
Author: User

The This keyword is used to represent the current object itself, or an instance of the current class, through which all methods and properties of this object can be called. For example:

  1. Public class Demo{
  2. public int x = ten;
  3. public int y = ;
  4. public void sum(){
  5. To take member variables by this point
  6. int Z = this. x + this. Y;
  7. System. Out. println("x + y =" + z);
  8. }
  9. Public static void main(String[] args) {
  10. Demo obj = New demo();
  11. Obj. Sum();
  12. }
  13. }

Operation Result:
X + y = 25

In the above program, obj is an instance of the Demo class, which is equivalent to obj, which executes int z = this.x + this.y, which is equivalent to executing int z = obj.x + obj.y;.

Note: This only makes sense if the class is instantiated.

Use this to distinguish a variable with the same name

How do you want to call member variables inside a method when the member variable has the same name as the variable inside the method? This can only be used at this time, for example:

  1. Public class Demo{
  2. public String name;
  3. public int age;
  4. public Demo(String name, int age){
  5. this. Name = name;
  6. this. Age = Age;
  7. }
  8. public void say(){
  9. System. Out. println("The name of the website is" + name + ", has been established" + Age + "Year");
  10. }
  11. public static void main(String[] args) {
  12. demo obj = New demo("Micro School", 3);
  13. Obj. Say();
  14. }
  15. }

Operation Result:
The name of the site is the Micro School, has been set up for 3 years

The scope of the formal parameter is the entire method body, which is a local variable. In the demo (), the names of the parameters and member variables are the same, and if you do not use this, the local variables name and age, not the member variables, are accessed. In Say (), we do not use this because the scope of the member variable is the entire instance, and of course it can be added as well:

    1. Public void say(){
    2. System. Out. println("The name of the website is" + this . Name + ", has been set up" + this . Age + "Year" );
    3. }

Java By default associates all member variables and member methods with this, so using this is redundant in some cases.

Initialize an object as a method name

This is equivalent to invoking the other constructor method of this class, which must be the first sentence of the constructor method. Examples are as follows:

  1. Public class Demo{
  2. public String name;
  3. public int age;
  4. public Demo(){
  5. this("micro-learning Court", 3);
  6. }
  7. public Demo(String name, int age){
  8. this. Name = name;
  9. this. Age = Age;
  10. }
  11. public void say(){
  12. System. Out. println("The name of the website is" + name + ", has been established" + Age + "Year");
  13. }
  14. public static void main(String[] args) {
  15. demo obj = New demo();
  16. Obj. Say();
  17. }
  18. }

Operation Result:
The name of the site is the Micro School, has been set up for 3 years

It is worth noting that:

    • Invoking another constructor method in the constructor method, the invocation action must be placed at the starting position.
    • You cannot call a construction method within any method other than the constructor method.
    • Only one constructor method can be called within a constructor method.


The above code involves method overloading, where Java allows multiple methods with the same name, as long as the parameters are different. A follow-up section will explain.

Pass as parameter

You need to call a method in some completely detached class and pass a reference to the current object as a parameter. For example:

  1. Public class Demo{
  2. public static void main(String[] args){
  3. b B = new b(new A());
  4. }
  5. }
  6. Class A{
  7. public A(){
  8. New B(this). Print(); //Anonymous object
  9. }
  10. public void print(){
  11. System. Out. println("Hello from a!" );
  12. }
  13. }
  14. Class B{
  15. a ;
  16. public B(a a){
  17. this. A = a;
  18. }
  19. public void print() {
  20. A. Print();
  21. System. Out. println("Hello from b!" );
  22. }
  23. }

Operation Result:
Hello from A!
Hello from B!

An anonymous object is an object without a name. If the object is used only once, it can be used as an anonymous object, in code new B (this). Print (); Equivalent to (new B (this)). Print (), first create an object without a name by using new B (this), and then call its method.

8.Java this keyword

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.