> What aren't we allowed to create a static variable inside a method in Java?

What aren't we allowed to create a static variable inside a method in Java?

Posted at: 2014-12-18 
Although C/C++ allow this, the designers of Java decided that this was not a feature they wanted in Java, so didn't allow it. Why? Not really sure. Probably just to keep the language simpler.

One interesting problem that would come up is: would static local variables be visible via reflection? If so, by what name? Suppose you have 2 functions with static boolean firstTiime=false;

If they aren't visible via reflection, object persistence becomes harder, because some object state variables can't be accessed via reflection.

Mostly because Java uses the usual rule that names are not visible outside the block they are declared in.

Besides, "static" doesn't mean "static" in Java. It means "belonging to the class", so inside the class--but not inside any method--is the correct place for definitions belonging to the whole class an not just a method.

Java has no static local variables such as you find in C or C++. The "static member of a class" use is similar enough, and probably makes some OOP theorists happier. I just put those declarations intended primarily for one method immediatly in front of the method (and its javadoc comments) so that it's not too far from where it would be if I was writing C.