Usage of the keyword this in Java

The role of this in Java is to refer to an object. According to the location of this, the main reference methods are divided into two situations:

  • Refer to the initialized object in the constructor in the constructor;
  • Refer to the object calling the method in the method

The biggest role of this is to allow another method or instance variable to be accessed by a method in the class;

public class DogText {
    
    
	//定义一个 jump 方法
	public  void jump()
	{
    
    
		System.out.println("正在执行 jump 方法!");
	}
	public void run()
	{
    
    
		//调用 jump 方法,构造器初始化;
		DogText d = new DogText();
		d.jump();
		System.out.println("正在执行 Run 方法");
	}
	
	public static void main(String[] args)
	{
    
    
		DogText d = new DogText();
		d.run();
	}

}

In the above example, the jump() method in the class is called in the run() method. Here, the DogText object is created to call this method, but in the main function main() function, another DogText object reference is created;

The above calling method can work normally, but the code running method is: 1. In the first method run(), calling other methods (such as jump() method) must create a DogText() object? 2. Is it necessary to recreate a DogText() object?

For the first question, yes, you must use an object to call a non-static modified method , so you need to create a new object, 2, not necessarily, because an object has been created when the main function run() is called, continue It can be called directly when in use and it is OK.

The run() method calls the jump() method, which can be directly used to obtain the object of the method in run(), and it can be satisfied with the keyword this

public void run()
	{
    
    
		//this 引用调用 run()的对象
		this.jump();
		System.out.println("正在执行 Run 方法");
	}

In Java, a method in a class is allowed to directly call another method, and the this prefix can be omitted, so the run() method can be rewritten as:

public void run()
	{
    
    
		//this 引用调用 run()的对象
		this.jump();
		System.out.println("正在执行 Run 方法");
	}

Most of the time, when a method calls another method, the effect of adding or not adding this to the instance variable is the same; although this is omitted, the object of the previous method call still exists;

One thing to note here is that static- modified methods cannot use the this keyword, because this keyword cannot point to a suitable object; therefore, static-modified methods cannot use this reference and can only be called by objects, which results in one of java Grammar requirements; static members cannot directly access static members ;

The case is as follows:

public class DogText {
    
    
	//定义一个 jump 方法
	public  void jump()
	{
    
    
		System.out.println("正在执行 jump 方法!");
	}
	public void run()
	{
    
    
		//调用 jump 方法,构造器初始化;
		DogText d = new DogText();
		d.jump();
		System.out.println("正在执行 Run 方法");
	}
	
	public static void main(String[] args)
	{
    
    
		//run() 程序报错,无法从静态上席文引用非静态
		DogText d = new DogText();
		d.run();
	}

}

Small summary

The above are some usages of the this keyword, I can summarize it in two points:

  • 1. Generally, the method in the class can directly call another method and member variable; this can be added or not, and it refers to the object;
  • 2. The static modification method cannot call the non-static modification method, and the constructor needs to construct a new object before calling it.

The article was first published in the public account for Java development enthusiasts

Guess you like

Origin blog.csdn.net/weixin_42512684/article/details/106813400