• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

compiling error with a variable declaration

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have the following code:


ok, so originally, line 5 was not there at all. However (i dunno why!!!) but I thought of doing what I did above and doing so generated a compile error:
-----------------------------------------------------

C:\code>javac SwitchCaseChar.java
SwitchCaseChar.java:5: illegal start of expression
public static final char character = 'b';
^
1 error
-----------------------------------------------------

however, when i removed public static final from the initialization on line 5, it just works fine.

Any idea as to why that is happening?

Thanks

Kiennjal
[ March 30, 2005: Message edited by: kiennjal shah ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The keywords "public" and "static" are meant to be used with member variables. A member variable is one that is declared in a class but outside of any method. When you declare a variable inside a method, as you are doing here, it is called a "local variable". You cannot use "public" or "static" with local variables.

Layne
 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it something to do with the local variables or the switch case requiring it to be a constant...?

Prolly something silly that I am overlooking?

Any input will be appreciated.

thx
 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Layne,

thanks, thats what i was thinking and by the time I posted my thoughts u had also replied.



-Kiennjal
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic