• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
 
Normally trees don't drive trucks. Does this tiny ad have a license?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic