메서드 레퍼런스에서 this 파라미터를 캡처할 수 있다. 예를 들어, this::equals는 x -> this.equals(x)와 같다. super도 사용할 수 있다. 다음 메서드 표현식은 this를 대상으로 해서 주어진 메서드의 슈퍼클래스 버전을 호출한다.
super::instanceMethod
다음은 이 기법을 보여주려고 만든 예제다.
class Greeter {
public void greet() {
System.out.println("Hello, world!");
}
}
class ConcurrentGreeter extends Greeter {
public void greet() {
Thread t = new Thread(super::greet);
t.start();
}
}
스레드가 시작할 때 해당 스레드의 Runnable이 호출되고 super::greet가 실행된다. 이때 슈퍼클래스의 greet 메서드가 호출된다.
<Note>
이너 클래스에서는 바깥쪽 클래스의 this 레퍼런스를 EnclosingClass.this::method 또는 EnclosingClass.super::method로 캡처할 수 있다.