Home > Java > Javagetting Started > body text

When to use this in java

(*-*)浩
Release: 2019-11-11 09:04:49
Original
3143 people have browsed it

This only exists inside the method and is used to represent the object that calls the method. It can be understood that there is a local variable called this inside each method. Whenever an object is initialized, the address of the object is passed to the this variable in each method of the object, so that this object can be used inside the method. .

When to use this in java

##The first situation (Recommended learning: java course)

In a general method, a formal parameter name in your method has the same name as a member of the current object. In order to avoid confusion, you need to explicitly use the this keyword to indicate that you want to use a certain For members, the usage method is "this. member name", and the one without this is the formal parameter. In addition, you can also use "this.method name" to refer to a method of the current object, but this is not necessary at this time. You can directly use the method name to access that method, and the compiler will know that you want to Which one is called.

public class DemoThis {
    private String name;
    private int age;
    DemoThis(String name, int age) {
        setName(name); // 你可以加上this来调用方法,像这样:this.setName(name);但这并不是必须的
        setAge(age);
        this.print();
    }
    public void setName(String name) {
        this.name = name;// 此处必须指明你要引用成员变量
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void print() {
        System.out.println("Name=" + name + " Age=" + age);// 在此行中并不需要用this,因为没有会导致混淆的东西
    }
    public static void main(String[] args) {
        DemoThis dt = new DemoThis("Kevin", "22");
    }
}
Copy after login

The second case

Suppose there are two classes, the container class Container and the content class Component. In the member method of Container, an object of the Component class needs to be called. . The constructor of Component requires a Container class that calls it as a parameter.

class Container{
    Component comp;
    public void addComponent(){
        comp=new Component(this);
    }
}
    class Component{
        Container myContainer;
        public Component(Container c){
            myContainer=c;
        }
    }
Copy after login

The third case

The construction method cannot be called like other methods and can only be called by the system when the system initializes an object. Although the constructor cannot be called by other functions, it can be called by other constructors of the class. In this case, just use this.

class Person{
    int age;
    String name;
    public Person(){
    }
    public Person(int age){
        this.age=age;
    }
    public Person(int age,String name){
        this(age);
        this.name=name;
    }
}
Copy after login

The above is the detailed content of When to use this in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!