Short answer: since the main method is static, it means that it belongs to the class, and not to a specific object. Static contexts cannot refer to a non-static context because it does not refer to a specific object.
But you could just make all your other variables and static methods too, and there the problem is solved, right?
No. Do not do that.
Long answer:
The main method is static , because none of your user defined application objects exist before it starts. After all, you cannot exist in a room before the room exists. However, the JVM needs an entry point to your program, so the main method should be free of all object references, hence static .
Java calls PersonProgram.main(args) same way your application starts. Now suppose you have determined that "Person" is in your PersonProgram class. Perhaps PersonProgram has a name, age, etc. But you don’t have any PersonProgram objects PersonProgram , since the main method knows what name or age it should work?
Here you can do something clearer: Create a new class called Application and put only your main method into it. Now try PersonProgram your PersonProgram instance PersonProgram . You cannot do this unless you first create a PersonProgram object.
You just started learning the concepts of OOP, and the idea of putting the main method in class definitions can be confusing. This makes it easier to learn the code, but you should try to keep your main method separate from your class logic when you try to learn the principles of OOP.
source share