File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes using Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using "final" inside an applet" Watch "using "final" inside an applet" New topic

using "final" inside an applet

Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36

Looking around, I see lots of people using the modifier final when declaring swing components like:

Is this a good practice? Is final used sparingly or liberally? Thanks!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Final has no cost associated with it in terms of performance or memory use, but from a software-engineering perspective, it has some nice benefits. This is pretty obvious, but a final variable's value can't change. Therefore, reading code that involves final variables is easier; you know more about the data involved than you would know if the variables weren't final.

In theory, at least, "final" allows the JVM to do more runtime optimizations, too: if you know a value will never change, for instance, you can cache it in a register rather than refetching it from memory.

So there are many plusses, and really no minuses. Using final whenever you can -- i.e., whenever you have a variable whose value will never change -- is a good practice.

[Jess in Action][AskingGoodQuestions]
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Thanks Ernest for a very clear answer
I agree. Here's the link:
subject: using "final" inside an applet
It's not a secret anymore!