Android中class.this与this的区别探究——维基百科App源码分析
CommunicationBridge.this instead of this in this lambda? Great question! Let's break down the difference between this and CommunicationBridge.this in this context, and why the code uses the latter.
First, the core context: Lambda vs. Anonymous Inner Classes
In your code snippet, the listener is defined using a lambda expression passed to addListener(). To understand the this behavior, we need to contrast it with how this works in anonymous inner classes (a common pattern before lambdas were introduced in Java 8).
1. What does this mean in a lambda?
In Java lambda expressions, this refers directly to the instance of the enclosing class (in this case, the CommunicationBridge instance being constructed). Unlike anonymous inner classes, lambdas don't create a new class instance—so there's no separate "lambda instance" that this would point to.
In theory, you could replace CommunicationBridge.this.webView with this.webView here, and it would work exactly the same way.
2. What does ClassName.this do?
ClassName.this is a syntax used to explicitly reference the instance of the outer class, especially when you're in a nested context where this might refer to a different instance.
The most common use case is in anonymous inner classes:
// Old anonymous inner class style this.addListener("DOMLoaded", new MessageListener() { @Override public void onMessage(String messageType, Object messagePayload) { isDOMReady = true; for (String jsString : pendingJSMessages) { // Here, `this` refers to the anonymous MessageListener instance, // so we need CommunicationBridge.this to access the outer class's webView CommunicationBridge.this.webView.loadUrl(jsString); } } });
In this case, using plain this would try to access webView on the anonymous listener instance (which doesn't exist), so CommunicationBridge.this is mandatory.
3. Why use CommunicationBridge.this in the lambda then?
There are two likely reasons for this choice in the code:
- Historical legacy: This code was probably originally written using an anonymous inner class, then refactored to use a lambda. The developer kept
CommunicationBridge.thisout of habit, or to maintain consistency with the old code. - Explicit clarity: Even though
thisworks in the lambda, prefixing with the class name makes it 100% clear to future developers that we're accessing the outerCommunicationBridgeinstance'swebView—no ambiguity, even if someone later modifies the surrounding code.
Key Takeaways for Android Development
- In top-level methods/constructors (not inside inner classes or lambdas),
thisandClassName.thisare identical—both refer to the current class instance. - In anonymous inner classes (or non-static nested classes),
thisrefers to the inner class instance. UseOuterClassName.thisto access the outer class's instance. - In lambda expressions,
thisalways refers to the enclosing class instance—soClassName.thisis optional, but can be used for clarity.
内容的提问来源于stack exchange,提问作者Shoaib Khan

