We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › "static final" results in illegal modifier error
Page Index Toggle Pages: 1
"static final" results in illegal modifier error (Read 927 times)
"static final" results in illegal modifier error
Jul 31st, 2009, 5:40pm
 
Hi all,

I'm in a bit of a pickle. I'm going through Ben Fry's Visualizing Data book (I'm on chapter 5) and I just ran into an error that has me stuck.  For those familiar with the book, I had the baseball example running up to the point of adding interaction.  

On to the problem.  My problem started as I was adding in code to complete the next section of the chapter.  When I clicked "Run", the code failed on the first constant variable I had which was something like
Code:
static final int ROW_HEIGHT = 23; 


This had worked fine through several iterations of refining the code while following along in the chapter.  This time, however, all I got was a nasty error, "Illegal modifier for parameter ROW_HEIGHT; only final is permitted."

Having just added some code, I thought that perhaps that may have caused the problem.  So, I commented out the new code.  Same error popped up.  Then I commented out everything but the offending line.  The same error still occured.  Next, I created a brand new sketch which contained only the single line:
Code:
static final int FOO = 42; 


This too failed in the same way.

Removing the static keyword does in fact allow the sketch to run, but I was under the impression that this is not the best practice in Java as without the static keyword, the variable can still be modified by the constructor.  Though that is unlikely in this situation, I am more concerned that if something is wonkers here, it may be wonkers elsewhere.

Sorry, but I'm a complete Processing and Java neophyte.  So let me know if there is more information needed.  I am using Processing 1.0.5 and some system information follows.

jonathan@jonathan-desktop:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 9.04
Release:        9.04
Codename:       jaunty
jonathan@jonathan-desktop:~$ uname -a
Linux jonathan-desktop 2.6.28-15-generic #48-Ubuntu SMP Wed Jul 29 08:53:35 UTC 2009 x86_64 GNU/Linux
jonathan@jonathan-desktop:~$ java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)
Re: "static final" results in illegal modifier error
Reply #1 - Aug 1st, 2009, 3:03am
 
true. But this will work:

Code:

static final int FOO = 42;

void setup() {
}


That's because if your sketch doesn't have a setup or a draw function, it's a static mode sketch.
Re: "static final" results in illegal modifier error
Reply #2 - Aug 1st, 2009, 3:07am
 
Indeed; this should run:

Code:
static final int FOO = 42; 

void setup() {
   
}


Processing does some stuff where it converts your top level code into a class and any classes as subclasses to that class (PhilHo is usually pretty good at explaining this stuff and will hopefully offer a more coherent explanation).  IIRC without the setup() call it doesn't behave properly which is probably why your single line didn't work.

If that doesn't work then I'd guess there's an issue with your installation of Processing or Java...
Re: "static final" results in illegal modifier error
Reply #3 - Aug 1st, 2009, 3:36am
 
Ah, I hit reply (after doing something else) and see already two replies in the summary... Smiley

So I will add my analysis.

First, I have no idea why you had the first error, perhaps the explanations below will help you finding the core problem.
Second, I want to comment on your belief: "without the static keyword, the variable can still be modified by the constructor". Not.
The final keyword states the variable is actually a constant, it can be initialized only at declaration time, in a static initialization block, or in the constructor of the class.
The static keyword means the variable will not be distinct for each instance (object) of the class, but shared by all the objects. A static variable can still be modified, all the objects will see the change.
A static final variable is a true constant, shared by all objects, kind of global variable, but confined in a scope (the class).

That's how Java works.
Now, when you type only a few lines in the PDE and run them, Processing will create a class (of same name as your sketch name), put a main() method in it, and if you don't provide a setup() method, will add it with your few lines in it.
That's why the static keyword raises an error, it is forbidden in a method...
If you add the setup() definition as done above, the declaration goes to top-level of the main class, and is legal there.

Note that you cannot declare a static variable in a class inside a .pde file because they are not allowed in a nested class (all classes in .pde file are internal to the main class). But in practice, we just put these variables at top level, as global variables. Or if we want to restrain the scope, we can put them in a class in a .java file, these classes go at same level as the main class.

I hope I am clear enough.
Re: "static final" results in illegal modifier error
Reply #4 - Aug 1st, 2009, 12:26pm
 
Thanks for everyone's replies, especially PhiLho's great explanation of the final keyword and the description of how Processing fits the sketches into the main class for Java.

It turns out my error was caused by a extraneous period after a semicolon later in the code.  It took a while to find because the period was just off screen as the line happened to be exactly the number of characters visible in the editor window with the semicolon as the last character.  When I saw the error, last night, I tried to narrow down the scope of the problem by commenting out all code past the offending line (that Processing complained about, not the actual error as it turns out).  This of course gave me the same error, but for the reasons blindfish and alvaro pointed out instead of the original cause.  This of course led me on a wild goose chase.

All in all, though, it turned out to be a rather educational typo.  Again, thanks for the insight and help.
Page Index Toggle Pages: 1