Why am I able to print this field in a static method?

George
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.

Boann

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

With CTE why i am not able to print 1 to 1000 number or more but 1 to 100 i am able to print

From Dev

Why am I not able to fetch database field values in a class using Jooq's into method?

From Dev

Why am i not able to print the whole character as i have entered it?

From Dev

Why am I not able to print the value outside th e for loop

From Dev

Why am I not able to use default method in interface?

From Dev

Why am I not able to change the value of the boolean variable in this method

From Dev

Why i am not able to call adapter method from my activity

From Dev

Why am I getting "An object reference is required for the non-static field, method, or property 'Photrax.App.DBPath.get'"?

From Dev

Why am I not Able to Construct This Regex

From Dev

Why am I not able to open a VS project

From Dev

Why am I not able to wget these types of links?

From Dev

Why am I not able to change worksheet reference?

From Dev

Why am I not able to add a JPanel to a JPanel?

From Dev

Why am I not able to use $this is an opencart helper?

From Dev

Why am I able to open this file?

From Dev

Why am I not able to add this route?

From Dev

Why am I not able to buy applications?

From Dev

Why I am able to override Equals method if my class doesn't inherit from anything?

From Dev

Why Am I Able to Use user() method in laravel, without even defining it

From Dev

Why am I able to modify a List in a method that's out of scope, without returning it?

From Dev

Why am I able to modify a List in a method that's out of scope, without returning it?

From Dev

Why am I getting an InvalidCastException when using an expression from a static field?

From Dev

Why am I able to duplicate entries in a mongodb using mongol by msavin even though I don't have a method allowing for that?

From Dev

I am not able to get a field in my Django ManyToMany Relation

From Dev

Why am I getting CS0119 using a static method in a template?

From Dev

I am not able to run an api using curl method on live server

From Dev

Why am I not able to consistently have a `HashTable` serialized?

From Dev

Why I am not able to populate the json data in bootstrap table

From Dev

Why am I not being able to compile SASS with Webpack?

Related Related

  1. 1

    With CTE why i am not able to print 1 to 1000 number or more but 1 to 100 i am able to print

  2. 2

    Why am I not able to fetch database field values in a class using Jooq's into method?

  3. 3

    Why am i not able to print the whole character as i have entered it?

  4. 4

    Why am I not able to print the value outside th e for loop

  5. 5

    Why am I not able to use default method in interface?

  6. 6

    Why am I not able to change the value of the boolean variable in this method

  7. 7

    Why i am not able to call adapter method from my activity

  8. 8

    Why am I getting "An object reference is required for the non-static field, method, or property 'Photrax.App.DBPath.get'"?

  9. 9

    Why am I not Able to Construct This Regex

  10. 10

    Why am I not able to open a VS project

  11. 11

    Why am I not able to wget these types of links?

  12. 12

    Why am I not able to change worksheet reference?

  13. 13

    Why am I not able to add a JPanel to a JPanel?

  14. 14

    Why am I not able to use $this is an opencart helper?

  15. 15

    Why am I able to open this file?

  16. 16

    Why am I not able to add this route?

  17. 17

    Why am I not able to buy applications?

  18. 18

    Why I am able to override Equals method if my class doesn't inherit from anything?

  19. 19

    Why Am I Able to Use user() method in laravel, without even defining it

  20. 20

    Why am I able to modify a List in a method that's out of scope, without returning it?

  21. 21

    Why am I able to modify a List in a method that's out of scope, without returning it?

  22. 22

    Why am I getting an InvalidCastException when using an expression from a static field?

  23. 23

    Why am I able to duplicate entries in a mongodb using mongol by msavin even though I don't have a method allowing for that?

  24. 24

    I am not able to get a field in my Django ManyToMany Relation

  25. 25

    Why am I getting CS0119 using a static method in a template?

  26. 26

    I am not able to run an api using curl method on live server

  27. 27

    Why am I not able to consistently have a `HashTable` serialized?

  28. 28

    Why I am not able to populate the json data in bootstrap table

  29. 29

    Why am I not being able to compile SASS with Webpack?

HotTag

Archive