How do I declare a static variable inside the Main method?

Priti picture Priti · Jul 31, 2010 · Viewed 66.4k times · Source

Can we declare Static Variables inside Main method? Because I am getting an error message:

Illegal Start of Expression

Answer

Roman picture Roman · Jul 31, 2010

Obviously, no, we can't.

In Java, static means that it's a variable/method of a class, it belongs to the whole class but not to one of its certain objects.

This means that static keyword can be used only in a 'class scope' i.e. it doesn't have any sense inside methods.