5
public class Main {
    private final int value = 3;

    public static Runnable buildRunner() {
        return new Runnable() {

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

    }
}

I am using Eclipse Kepler, with JRE 7.

In the buildRunner method - why I am able to see the this of Main? What is the 'this' of Main in a static method? Why does this compile?

I can only do that if value is final. I cannot call instance methods of Main and stuff, but value is not decalred static! Furthermore, if I want to use in the buildRunner method, outside the run method of the new Runnable, the compiler stops me from doing that.

13
  • 3
    Are you sure this exact code compiles ? javac will not compile it (Main.java:9: error: non-static variable this cannot be referenced from a static context)
    – nos
    Jul 3, 2014 at 23:43
  • 1
    The code in not valid. Looks like a bug in Eclipse compiler.
    – apangin
    Jul 3, 2014 at 23:44
  • You aren't. This won't even compile. Jul 3, 2014 at 23:45
  • using this inside run method is pointing to the Runnable object
    – Shadi
    Jul 3, 2014 at 23:48
  • 2
    @user3580294 Bug reported at bugs.eclipse.org/bugs/show_bug.cgi?id=438890
    – George
    Jul 4, 2014 at 0:26

1 Answer 1

4

JLS § 15.8.4 says "The value of an expression of the form TypeName.this is the n'th lexically enclosing instance of this". Since there is no enclosing instance of Main in your example, the code is not valid.

In javac, the code produces the error "non-static variable this cannot be referenced from a static context". The fact that it compiles in Eclipse appears to be an obscure bug in Eclipse (which has its own compiler).

6
  • 2
    It does not just comple. It runs and prints out 3.
    – George
    Jul 3, 2014 at 23:50
  • 1
    @George Just a bad optimization that incorrectly replaces inaccessible final field with a compile-time constant.
    – apangin
    Jul 3, 2014 at 23:51
  • @George - That makes no sense. Fix your code so that it can compile from the command line. Then run it, and if there are any errors, then you can come back to ask a valid question. I would strongly suggest you get used to compile from the command line. The command line never lies ;) Jul 3, 2014 at 23:51
  • Other than that, Boann's post should be accepted as an answer (since it does answer why your code "sees" the this reference from the static method). Jul 3, 2014 at 23:53
  • 1
    @ChiefTwoPencils It's a bug, although it might have been caused by an optimization attempt. If it were intended to work, then Main.this.value should also be readable from within a static method of Main without wrapping the expression in an inner Runnable, but if you try that, then Eclipse says: "Cannot use this in static context".
    – Boann
    Jul 4, 2014 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.