• Post Reply Bookmark Topic Watch Topic
  • New Topic

compiling error with a variable declaration  RSS feed

 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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 ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!