+ Start a Discussion
Edward chanEdward chan 

Unable to declare it

Hi,
Here are the codes I have:

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


why?

Prafull G.Prafull G.

shop() method has a varibale message which is local and your code uses public identifier.

 

Please update the variable declaration as "String message;" i.e. remove 'public'

 

Regards,

-P

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Edward Chan,

 

Under the public method you are declaring public member. Thats why it throwing error.

 

Replace you code with following.

public class StoreFront {
  //  public String message;  If you want to declare message as a public then declare outside of Shop() method.
    public PageReference shop() {
        String message;           //Local variable can not be Public.
        message = 'You bought: ';
        for (DisplayMerchandise p: products) {
            if (p.count > 0) {
                message += p.merchandise.name + ' (' + p.count + ') ';
            }
        }
    return null;
    }
}

 

R DraperR Draper

Hi all, I have  a weird note to add to this thread.

 

I declared this in a trigger body before the winter 14 upgrade:

 

private List<FieldSetMember> fieldSetMembers;

 and it saved fine, compiled and the trigger worked perfectly.

 

Now that I am revisiting the trigger to make some edits I get an error for Illegal modifier, which I understand, I just missed it when I was coding originally.  I removed the 'private' and it saved again.  My question is why did it let me save in the first place?  Must be an api version thing.