java this class,java中.this和.class的含义

本文详细解释了在Java编程中,`this`关键字和`.class`表达式的区别。`this`引用当前类的实例,常用于区分内部类中的成员。而`.class`返回一个Class对象,代表类本身,常用于反射操作。了解这两者的用法对于理解和调试Java代码至关重要。
摘要由CSDN通过智能技术生成

Let's say we have a class name Home. What is the difference between Home.this and Home.class? What do they refer to?

解决方案

Home.this

Home.this refers to the current instance of the Home class.

The formal term for this expression appears to be the Qualified this, as referenced in Section 15.8.4 of the Java Language Specification.

In a simple class, saying Home.this and this will be equivalent. This expression is only used in cases where there is an inner class, and one needs to refer to the enclosing class.

For example:

class Hello {

class World {

public void doSomething() {

Hello.this.doAnotherThing();

// Here, "this" alone would refer to the instance of

// the World class, so one needs to specify that the

// instance of the Hello class is what is being

// referred to.

}

}

public void doAnotherThing() {

}

}

Home.class

Home.class will return the representation of the Home class as a Class object.

The formal term for this expression is the class literal, as referenced in Section 15.8.2 of the Java Language Specification.

In most cases, this expression is used when one is using reflection, and needs a way to refer to the class itself rather than an instance of the class.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值