LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-20-2013, 10:15 PM   #1
Huamin
Member
 
Registered: May 2011
Posts: 520

Rep: Reputation: Disabled
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?
 
Old 03-20-2013, 11:35 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
Hi,

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

Evo2.
 
Old 03-21-2013, 12:01 AM   #3
Huamin
Member
 
Registered: May 2011
Posts: 520

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-21-2013, 01:34 AM   #4
derstephen
Member
 
Registered: Sep 2011
Location: Northern Colorado
Distribution: Slackware64
Posts: 79

Rep: Reputation: Disabled
Quote:
Originally Posted by Huamin View Post
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.

Last edited by derstephen; 03-21-2013 at 01:50 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] unable to declare array in Solaris ankitpandey Programming 10 12-27-2012 05:19 PM
Let's declare war on the ocean! icecubeflower General 29 04-16-2010 04:54 PM
Unable to declare a variable in Cygwin erora Linux - Newbie 4 01-21-2010 04:27 PM
Declare Vs Define Ace Blackwell Programming 15 09-14-2008 09:23 PM
declaration does not declare anything tristanm Programming 5 10-24-2005 05:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration