Home > Java > Javagetting Started > How to understand this in java

How to understand this in java

(*-*)浩
Release: 2019-11-14 09:25:08
Original
2579 people have browsed it

How to understand this in java

#java provides a this keyword, which always points to the object that calls the method. Depending on where this appears, there are two situations where this serves as the default reference to the object.                                                                   (Recommended learning: java course )

a), The constructor refers to the object being initialized by the constructor. (this always refers to the object being initialized by the constructor. For example, if local variables and global variables have the same name, this always refers to the local variables of the constructor).

b), Reference the object that calls the method in the method.

The biggest function of this keyword is to allow a method in a class to access another method or instance variable of the class. (Java allows one member of an object to directly call another member, and the this prefix can be omitted. Most of the time, the effect of adding or not adding the this prefix when a method accesses other methods and member variables defined in the class is exactly the same).

This can represent any object. When this appears in a method body, the object it represents is uncertain, but its type is determined. The object it represents can only be the current class. . Only when this method is called, the object it represents is determined. Who is calling this method, this represents who.

For static-modified methods, you can use a class to directly call the method. If the this keyword is used in a statis-modified method, this keyword cannot point to the appropriate object.

So, this reference cannot be used in statically modified methods. Since static-modified methods cannot use this reference, static-modified methods cannot access ordinary members that are not static-modified. Therefore, Java grammar stipulates that static members cannot directly access non-static members.

package com.bie.main;

/**
 * 
 * @Description TODO
 * @author biehl
 * @Date 2019年3月9日 上午10:53:48
 * 
 */
public class Person {

    public int age;//成员变量。
    
    public Person() {
        //构造器中定义一个age局部变量
        int age = 0;
        //使用this代表该构造器正在初始化的对象。
        //将构造器正在初始化的对象age对象设置为25
        this.age = 25;
    }
    
    public static void main(String[] args) {
        System.out.println(new Person().age);
    }
}
Copy after login

The above is the detailed content of How to understand 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