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 float
Page Index Toggle Pages: 1
static final float (Read 442 times)
static final float
Jul 2nd, 2009, 11:57pm
 
Hi, is there any difference between doing "static final float " and "float" for declaring a variable?

ive seen some programs that uses both in the same code, why is that?
which are the differences?

many thanks
Re: static final float
Reply #1 - Jul 3rd, 2009, 12:50am
 
these are really harcdcore Object Oriented programming subtleties, but keeping things down-to-earth, static final float is a constant, and just float is a variable.
Re: static final float
Reply #2 - Jul 3rd, 2009, 1:59am
 
if you stay inside pde (ie: you don't develop under eclipse or netbeans), you keep your sketch simple and you don't integrate your sketch inside a bigger java project, you won't probably crash into static stuff.

on the other side, learning how to correctly declare a variable (or function) can save you a few headaches later Smiley so here we go:

- static: a 'static' method or variable is not linked to a single object but is attached to a whole class. i know this sounds a little exoteric the first time you hear it, so i'll try to make it easier with and example: suppose you create a class like this:
Code:
class Example{
int counter;
...
Exampe(){
counter++;
println(counter);
}
...
}

then instantiate a few objects:
Code:

...
obj1 = new Example();
obj2 = new Example();
obj3 = new Example();
...

now, in the console you'll see 3 lines containg the same value right? each object creates its 'counter' and increments it the same way.
things would be differente if the counter was satic:
Code:

class Example{
static int counter;
...
Exampe(){
counter++;
println(counter);
}
...
}
...
obj1 = new Example();
obj2 = new Example();
obj3 = new Example();
...

now in the console you'll read something like
Code:

1
2
3

because the static int counter deals directly with the example class and has nothing to do with single objects. obj1's constructor increments counter from 0 to 1, so when obj2's constructor is called counter is equal to 1, and is incremented to 2, and so on.

- final: this keyword means that the variable cannot be changed, making its behaviour similar to a constant
Re: static final float
Reply #3 - Jul 3rd, 2009, 2:09am
 
Would another way to put it be that a 'static' property is effectively global for all members of the class?

Also IIRC you can only declare 'static final' properties (not just 'static') in classes created in Processing.
Re: static final float
Reply #4 - Jul 3rd, 2009, 2:25am
 
Hi

thanks naus3a for your example. Just digging deeper, I found out final is not enough to make a variable constant, since the value can be changed in the constructor (and only there), like this:

Code:

class Example2 {
   final double c; // <-- NOT a constant
   
   Example2() {
       c = random(100);
       println(c);
   }
   
}

Example2 obj1 = new Example2();
Example2 obj2 = new Example2();
Example2 obj3 = new Example2();


Re: static final float
Reply #5 - Jul 3rd, 2009, 3:25am
 
alvaro wrote on Jul 3rd, 2009, 2:25am:
I found out final is not enough to make a variable constant, since the value can be changed in the constructor (and only there)

It is constant for your class instance. It has no other value, not even default one, so during all its lifetime, it is constant.
If you set c to a value in the definition, you will get an error: "The final field Example2.c cannot be assigned".
If you try to look at its value before it is set in the constructor, you will get another error: "The blank final field c might not have been initialized"
I get the latter when trying to access it in a static init block:
Code:
class Example2 {
   final double c;
   
   {
println(c);
   }
   
   Example2() {
 c = random(100);
 println(c);
   }
}

So the constant c might not be the same for all classes (for this case, you make it static), you can even give its value in the parameter of the constructor, but you cannot change its value later, that's why it is seen as constant.

blindfish: indeed, you cannot make plain static fields in classes in .pde files, because all these classes are internal to the main class (inner). Thus they are effectively shared among all instances... (but accessible to other classes Sad).
The workaround is of course to make them "global" variables (ie. fields of the main PApplet class). Or to declare them in .java files.
Page Index Toggle Pages: 1