Java "This" reference(引用)

When you instantiate an object from a class, memory is reserved for each instance field in the class. For example, if a class contains 20 data fields, when you create one object from that class, enough memory is reserved to hold 20 values for that object. Fortunately, it is not necessary to store a separate copy of each variable and method for each instantiation of a class. Usually, you want each instantiation of a class to have its own data fields. When you want each object to have a unique field value, you do not define the field as static. When you create a method for a class, any object can use the same method. Luckily, in Java just one copy of each method in a class is stored, and all instantiated objects can use that copy.

Every object in Java is uniquely identified by its this reference. The this reference is the “handle”(pointer) to the object that is used to invoke methods or explicitly reference instance variables.

The “this” reference is the mechanism that is used to tie them together at run-time. The this reference as a hidden first argument to every non-static class method. Static “class” methods do not have a this reference passed to them.

It is not possible to refer to the this variable in a static method of a class.

class Foo {

   int x;

   static int f() { return this.x; } // ERROR

}

Commonly static methods are used all the time in Java.

import java.net.*; // need to import InetAddress class

publicclass Host {

    static String host;

    static String user;

    staticfinalinterr = -1; // same as const int err = -1 in C++

    publicstaticvoid main (String[] args)

    {

        try {

            InetAddress addr = InetAddress.getLocalHost();

            host = addr.getHostName();

            user = System.getProperty("user.name");

            System.out.println(user + "@" + host); // string concatenation

        }catch (Exception e) {

            System.err.println(e); System.exit(err);

        }

    }

}

Output:

mz50947@APACCNSHZJN0764

The System.getProperty method is a static method defined as part of the java.lang.System class that returns a String containing the value of system property “user.name”.

在lambda表达式中,this关键字的作用与普通的Java类中的this关键字不同。在lambda表达式中,this关键字指向创建lambda表达式的方法所在的类实例。 这与使用Runnable接口的匿名类不同,匿名类中的this关键字指向匿名类实例本身。因此,在使用Runnable接口时,this关键字可以正常使用,但在lambda表达式中,使用this关键字可能会出现问题。 这是由编译器的实现方式决定的。编译器会为lambda表达式生成一个类,并将其作为一个方法传递给函数式接口。因此,lambda表达式中的this指向的是创建lambda表达式的方法所在的类实例,而不是lambda表达式本身。 所以,当你在lambda表达式中使用this关键字时,请确保你理解它指向的是哪个对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Java细节,Lambda表达式中使用this会怎么样?](https://blog.youkuaiyun.com/lfq88/article/details/113697991)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java8()Lambda表达式中的this](https://blog.youkuaiyun.com/u010825931/article/details/105774383)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值