Skip to content
CodeKruPro

this inside a lambda and an anonymous class

Predict the outputMedium-HardJava only
Main.java
public class Main {
    String name = "outer";

    void run() {
        Runnable lambda = () -> System.out.println(this.name);
        Runnable anon = new Runnable() {
            String name = "anonymous";

            @Override
            public void run() {
                System.out.println(this.name);
                System.out.println(Main.this.name);
            }
        };
        lambda.run();
        anon.run();
    }

    public static void main(String[] args) {
        new Main().run();
    }
}

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in