LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Unable to declare it (https://www.linuxquestions.org/questions/programming-9/unable-to-declare-it-4175454921/)

Huamin 03-20-2013 09:15 PM

Unable to declare it
 
Hi,
I get this error
Error: Compile Error: Illegal modifier on local variable at line 3 column 15

due to
public String message;

why?

evo2 03-20-2013 10:35 PM

Hi,

perhaps you could provide some more information. Eg what language, and perhaps even show us the code.

Evo2.

Huamin 03-20-2013 11:01 PM

Yes, here are the codes in Java:

1 public class StoreFront {
2 public PageReference shop() {
3 public String message;
4 message = 'You bought: ';
5 for (DisplayMerchandise p: products) {
6 if (p.count > 0) {
7 message += p.merchandise.name + ' (' + p.count + ') ';
8 }
9 }
10 return null;
11 }
...

and here is the error
Error: Compile Error: Illegal modifier on local variable at line 3 column 15

derstephen 03-21-2013 12:34 AM

Quote:

Originally Posted by Huamin (Post 4915664)
Yes, here are the codes in Java:

1 public class StoreFront {
2 public PageReference shop() {
3 public String message;
4 message = 'You bought: ';
5 for (DisplayMerchandise p: products) {
6 if (p.count > 0) {
7 message += p.merchandise.name + ' (' + p.count + ') ';
8 }
9 }
10 return null;
11 }
...

and here is the error
Error: Compile Error: Illegal modifier on local variable at line 3 column 15

It's been a while since I've used Java, but it looks like you're declaring an automatic stack variable (message) with the "public" modifier. The public/private/protected modifiers only apply to data members and methods of a class, not local variables within a function/method.

The easiest fix would probably be to just remove 'public' from the message declaration (so line 3 would just read "String message;"). Or if you really do intend to have the message string be a public member of the StoreFront class (although in this case it seems unlikely) you could switch lines 2 and 3 (and keep the 'public' modifier).

Basically the compiler is saying that the words 'public', 'private', and 'protected' are not allowed once you're inside a method.


All times are GMT -5. The time now is 01:27 PM.