---
title: "The key word \"this\" in java."
author: "BBjava"
date: "2011-08-07T11:54:34Z"
tags: []
categories: ["编程语言"]
slug: "the-key-word-this-in-java"
draft: false
lastmod: "2011-08-07T11:54:34Z"
id: 1141903
url: "https://bbjava.iteye.com/blog/1141903"
---

今天早上突然想起来java中有个key word叫this,以前都没怎么注意，于是写下帮助记忆。

```java
package org.iteye.bbjava.thistest;

public class ThisTest {
	private String thisStr="The key word 'this'!";
	public ThisTest(){
		
	}
	
	//this引用构造方法
	public ThisTest(String thisStr){
		this();
		this.thisStr=thisStr;//特别指出，当前使用的是实列变量，而不是静态变量或局部变量。
	}
	public ThisTest(UseThisTest utt){
		System.out.println("Hello!my name is "+utt);
	}
	
	//
	public void test(){
		System.out.println(this.thisStr);
		System.out.println("The key word 'this' in the ThisTest class:  "+this);
	}
	
	//返回当前实列
	public ThisTest returnMyself(){
		return this;
	}
	
	
	public static void main(String []args){
		ThisTest tt=new ThisTest();
		tt.test();
		System.out.println("returnMyself:"+tt.returnMyself());
	}
}

```


output

> The key word 'this'!
> The key word 'this' in the ThisTest class:  org.iteye.bbjava.thistest.ThisTest@de6ced
> returnMyself:org.iteye.bbjava.thistest.ThisTest@de6ced
> 



```java

package org.iteye.bbjava.thistest;

public class UseThisTest {
	private ThisTest tt = new ThisTest();
	
	public void test(){
		System.out.println("Do nothing!");
	}
	public void callTest(){
		
		ThisTest utt1 = new ThisTest(this);//把当前对象作为参数传传递。
		this.tt.test();
		System.out.println("The key word 'this' in  the UseThisTest class:"+this);
	}
	public static void main(String []args0){
		UseThisTest utt = new UseThisTest();	
		utt.test();
		utt.callTest();
	}
}


```


output:
> Do nothing!
> Hello!my name is org.iteye.bbjava.thistest.UseThisTest@c17164
> The key word 'this'!
> The key word this in ThisTest:org.iteye.bbjava.thistest.ThisTest@1fb8ee3
> The key word 'this' in the UseThisTest class:org.iteye.bbjava.thistest.UseThisTest@c17164
>