• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why "Illegal start of expression"?  RSS feed

 
Sank Shah
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


enum Grades
{
Fail("<40"),Pass("<60"),FirstClass("<75"),Distinction(">=75");
Grades(String ss){s=ss;}
String s;
}
class enumoc
{
/*COMMENT A*/ static Grades grades;
public static void main(String args[])
{
//COMMENT B
System.out.println("Grades are "+grades.values());
}

}

Please help
 
M K Rayapudi
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean, you want to move that declaration to main() method.


local variables should not be declared as static, that's why you are getting that compilation error.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!