• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Why "Illegal start of expression"?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Hey cool! They got a blimp! But I have a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic