The following code works perfectly fine if i use it as it is. If I move the code at comment A to comment B, I get error "Illegal start of expression" on compiling. Why is it so?
The problem gets solved if after moving I remove static from the code moved and then change "grades" to "Grades" in print statement which is understandable as "grades" no longer remains static & the code moved becomes redundant.
But why doesnt the file compile if code is moved from A to B?
/*COMMENT A*/ static Grades grades;
public static void main(String args)
System.out.println("Grades are "+grades.values());
Welcome to JavaRanch. Please use code tags when you post code, that makes your source code much more readable.
The point at comment B is inside the main method. If you put a variable declaration there, the variable will be a local variable inside the main method. (At point A it is a member variable of class enumoc).
Local variables inside methods cannot be static, if you put "static Grades grades;" at comment B, the compiler will complain that it doesn't know what "static" means in that context - that's why you get an error message.