Java's usage of the this keyword, javathis keyword

Source: Internet
Author: User

Java's usage of the this keyword, javathis keyword

The this keyword in Java indicates the current object. You can use it to call the member variables of the current object or call the member methods of the current object directly. this is a common scenario, is there any other situation!

This can also directly enclose parentheses in the constructor without parameters to call the constructor with parameters. In this way, when we create a new object, instead of initializing through the construction method with parameters, you can directly use the construction method without parameters.

The Code is as follows:

Public class Student {private String name = "Zhang San"; private int sum = 0; public Student () {this ("Wang Wu "); // call the construction method with parameters} public Student (String name) {this. name = name; // call the name in the member variable to distinguish the name} public void say () {System. out. println (this. name);}/*** recursive summation * @ param num */public void sum (int num) {this. sum + = num --; if (0 <num) {sum (num);} else {System. out. println ("sum =" + sum); this. say (); // call the member method say ()}}}

 

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.