0

Why this keyword cannot be used in static method.

   class Hello{ 
    public static void main(String[] args) { 
    System.out.println(this); 
    }
   }

1 Answers1

2

The main method is static and therefore cannot be accessed with the keyword this :)

Note: Indeed, the question has been answered already : here

Community
  • 1
  • 1
Floran Gmehlin
  • 824
  • 1
  • 11
  • 34
  • In more gentle words: when you have another questions which also answers the current one, then flag it as a duplicate instead of writing the same stuff as an answer. – Tom Sep 22 '16 at 09:34
  • @Tom Thanks for pointing that out. Nobody here is farming reputation, but more like helping people. I didn't even know how to flag for duplicate until you mentioned that. – Floran Gmehlin Sep 22 '16 at 09:36
  • 1
    *"Nobody here is farming reputation"* A lot of people do (sadly). When you're not one of them, then that is really nice to hear. Help page about flagging: http://stackoverflow.com/help/privileges/flag-posts – Tom Sep 22 '16 at 09:43
  • Mine is grown organically. – Kayaman Sep 22 '16 at 09:46
  • The person is not `accessing` the main method with the `this` keyword. It would mean doing this: `this.main();`. What the OP does is absolutely nonsensical. He's trying to display the value of a (final) object, which is impossible. – progyammer Sep 22 '16 at 10:02