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 [CLEARLY EXPLAINED]
Page Index Toggle Pages: 1
Static Final [CLEARLY EXPLAINED] (Read 1165 times)
Static Final [CLEARLY EXPLAINED]
Jul 17th, 2008, 10:24am
 
I saw some programs declaring variable, function and class using Static and/or Final.
This article  http://en.wikipedia.org/wiki/Final_(Java) explains it but I am not sure to understand.

Is it for optimization purpose, because it seems that you can't change a static final float? So that it's fixed once and for all and the 'program' doesn't have to reserve processing power in case this variable is going to be change.

Thanks

   
Re: Static Final: What's the purpose?
Reply #1 - Jul 17th, 2008, 11:18am
 
Making a variable 'final' is as much to do with safe programming as run-time efficiency. To understand why, you need to appreciate the difference between classes and objects.

Suppose  I have a class called MyCircle that contains within it a non-static variable defining a circle's radius.

You could create many instances from that class, each of which could have its own radius. The value of each object's radius is completely independent of any other object's radius value. ie. changing the value of one object's radius has no effect on the values stored by other objects that come from the same MyCircle class. This is the normal way of doing things in object-oriented programming.

If you were instead to define the radius variable as static, this would mean that the variable is associated with the class, not each of the objects created from it. As a consequence only one copy of it is stored, even if many objects are created. Change the value of radius in one object, and all the other objects' radius values change too. This is poor object-oriented practice and could lead to confusing bugs in complex programs. By making a static variable final, this ensures that once declared, its value can never be changed, thus reducing the chances of hard to trace bugs (you'd get an easy to spot compiler error if your code tried to change the value of a final variable).

In practice it therefore only makes sense to declare final static variables to represent constant values that never change at run-time (e.g. a program that contains a large collection of circles all of a constant radius). By convention, static final variables are given upper case values to distinguish them from true variables.

The story with final methods and classes is slightly more complicated, but is essentially the same principle of keeping behaviour fixed so that a class or object does not do unexpected things in your program.
Re: Static Final: What's the purpose?
Reply #2 - Jul 17th, 2008, 11:45am
 
Brilliant, thanks you for your time and clear explanation.
What I don't get is that if you want all objects to use the same radius variable. Why not just declare it at the beginning like "normal" instead of Static.

--------------------------------------
float radius

void setup(){
}
void draw(){
//objects drawing
}

class cercle{
ellipse(x, y, radius, radius);
etc...
}
--------------------------------

Is it so you can declare your radius in the class? Is it some kind of nit code practice so everything stay in the class, like that:

--------------------------------
void setup(){
}
void draw(){
}
class cercle{
Static float radius = 10;
void update(){
ellipse(x, y, radius, radius);
}
etc...
}

---------------------------------

Otherwise it seam really useful for debug and clarity to use final if you know that you won't change the var. Thanks a lot, I have learn something more today.

Cheers
Re: Static Final: What's the purpose?
Reply #3 - Jul 17th, 2008, 12:13pm
 
Your first example is a perfectly reasonable approach to use in Processing. By declaring the variable radius at the beginning like that you have effectively made it 'global'. ie. its value can be changed both inside and outside your cercle* class.

The second example is also fine (assuming you add a 'final' to the static declaration). This case is an example of encapsulation where, as you say, everything that relates to the properties of the circle are placed inside the class that represents it.

Which of the two approaches you take will depend on

(i) the complexity of the program. Simple programs can use the first 'global' approach without much problem. Processing is set up to make this approach really simple and easy to implement quickly. As programs become more complex, using global variables in this way can make debugging more complicated and difficult to trace though.

(ii) What parts of your program need access to 'radius'. If a circle's radius is something that only the circle class need ever have access to, then it makes sense to encapsulate it inside the class. That helps to keep the non-circle part of your code cleaner and easier to follow. If radius is something that is shared between many different parts of your code, then it may be better to  declare it elsewhere, such as in your first example.


* One minor point: Classes should always be named with an initial capital letter. This is a programming convention rather than a compiler issue, but it helps to differentiate classes from objects (that should always be named with an initial lower-case latter). Doing so will help to embed these ideas of classes, objects, static and instance variables etc.
Re: Static Final: What's the purpose?
Reply #4 - Jul 17th, 2008, 1:18pm
 
Thanks a lot for teaching me (us) all that. You are really clear in your text. All that make great sense.

Quote:
*One minor point: Classes should always be named with an initial capital letter. This is a programming convention rather than a compiler issue, but it helps to differentiate classes from objects (that should always be named with an initial lower-case latter).

It's funny I always did the opposite. oh well...
Are you a teacher by the way?

UPDATE:
is that your book? http://www.amazon.com/Java-Programming-Spatial-Sciences-Wood/dp/0415260973
Re: Static Final [CLEARLY EXPLAINED]
Reply #5 - Jul 17th, 2008, 1:30pm
 
Yup, that's me. In fact I was working on text for the second edition of that book today before getting distracted by Processing...
Re: Static Final [CLEARLY EXPLAINED]
Reply #6 - Jul 17th, 2008, 2:16pm
 
naughty processing.
Re: Static Final [CLEARLY EXPLAINED]
Reply #7 - Jul 22nd, 2008, 5:57am
 
That was amazingly well explained Smiley Here are my thoughts on statics and finals..

- making something static, and especially static final will speed up things, I have proven this (in J2ME atleast) - due to the less checking required during run time, ie, no checking at all if its final - I guess the compiler does some nice jiggery pokery also since its a "constant" effectively if its final. Even though I could get burned at the steak for suggesting this, when I need to optimise, I make everything static and static final if I can - SO LONG AS THIS WILL NOT AFFECT anything in my run time - ie, you know that there is only ever once instance of a class.

- the same approach can save jar size and make life easier, again heretic speak but I often cant be bothered to code a getVal() setVal() so I just make it public static and get it by doing ClassName.myStaticVar - it is a hell of a lot quicker than making get/set methods or even trying to find the instance name and doing instance.myVar. Again ONLY IF THIS WONT AFFECT the runtime of my code. You may argue this is bad practise for later when I upgrade my code, yep thats fair enough.

- As Jo mentioned statics should be done in capital letters, mainly so your brain goes ohh CAPS must be static (im learning all these conventions are really clever because you save so much time not checking what stuff is - similarly if you keep all your variables named the same sort of thign throughout your career in the end youll be able to "guess" what you called it without going looking) - using these finals in things that get called a lot can speed things up, like in a constantly called draw method:

getWidth()/3.142

is blatently going to be slower than:

WIDTH/PI

or even:

WIDTH_OVER_PI (fastest)

in switch statements its nice do use finals, as the states will enver change, ie:

switch(gameMode)
{
case INTRO:
break
case CREDITS:
}

which helps code readability..

Anyway just thought id stick my 2 cents in there in case it helps.. I have a tendency to tell people stuff how it is, rather than the academic way and sometimes I feel "human beings" prefer it that way Smiley

Thats not to say Jo is not a human being, I loved the explanations, Im just talking about these Java purists who would have me hanged for saying yeh I make stuff static final for speed and ease... (mostly theyre hiding on IRC Smiley

Cheers,
Gaz.
Page Index Toggle Pages: 1