重温 Thinking in Java - 3 The this keyword

本文深入探讨了Java中this关键字的用途及重要性,包括如何使用this关键字引用当前对象,从构造器调用其他构造器的方法,以及理解静态方法中this关键字的缺失。通过实例展示了this关键字在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

class Banana {
	void peel(int i){
		//do something
	}
}

public class BananaPeel {
	public static void main(String[] args){
		Banana a = new Banana(),
			b = new Banana();
			
		a.peel(1);
		a.peel(2);
	}
}

If there's only one method called peel(), how can that method know whether it's being called for the object a or b?

 

There's a secret first argument passed to the method peel(), and that argument is the reference to the object that's being manipulated. So the two method calls become something like:

Banana.peel(a, 1);
Banana.peel(b, 2);

Suppose you're inside a method and you'd like to get the reference to the current object. Since that reference is passed secretly by the compiler, there's no identifier for it. However, for this purpose there's a keyword: this . The this keyword - which can be used only inside a non-static mehtod - produces the reference to the object that the method has been called for. You can treat the reference just like any other object reference. Keep in mind that if you're calling a method of your class from within another method of your class, you don't need to use this. You simply call the method. The current this reference is automatically used for the other method.

public class Apricot {
    void pick() {/* ... */}
    void pit() {
        pick(); /*...*/
    }
}

Inside pit(), you could say this.pick() but there's no need to. The this keyword is used only for those special cases in which you need to explicitly use the reference to the current object.

 

The this keyword is also useful for passing the current object to another method.

 

Calling constructors from constructors

 

When you write several constructors for a class, there are times when you'd like to call one constructor from another to avoid duplicating code. You can make such a call by using the this keyword.

 

Normally, when you say this, it is in the sense of "this object" or "the current object," and by itself it produces the reference to the current object. In a constructor, the this keyword takes on a different meaning when you give it an argument list. It makes an explicit call to the constructor that matches that argument list.

 

public class Flower {
	int petalCount = 0;
	String s = "initial value";
	
	Flower(int petals){
		petalCount = petals;
		System.out.println("Constructor w/ int arg only, petalCount = " + petalCount);
	}
	
	Flower(String ss){
		System.out.println("Constructor w/ Stirng arg only, s = " + ss);
		s = ss;
	}
	
	Flower(String s, int petals){
		this(petals);
		//! this(s); //Cannot call two!
		this.s = s;//Another use of "this";
		System.out.println("String & int args");
	}
	
	Flower(){
		this("hi", 47);
		System.out.println("default constructor (no args)");
	}
	
	void PrintPetalCount(){
		//! this(11);// Not inside non-constructor!
		System.out.println("petalCount = " + petalCount + " s = " + s);
	}
	
	public static void main(String[] args){
		Flower x = new Flower();
		x.printPetalCount();
	}
}

 The constructor call must be the first thing you do, or you'll get a complier error message.

 

The meaning of static

 

With the this keyword in mind, you can more fully understand what is means to make a method static. It means that there is no this for that particular method. You cannot call non-static methods from inside static methods (although the reverse is possible), and you can call a static method for the class itself, without any object. In fact, that's primarily what a static method is for. It's as if you're creating the equivalent of a global method. However, global methods are not permitted in Java, and putting the static method inside a class allows it access to other static methods and to static fields.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值