my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Static vs. Final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static vs. Final" Watch "Static vs. Final" New topic
Author

Static vs. Final

Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hello All, I am wondering when it is appropriate to use Static and/or Final and what the difference is. TIA.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
That is a bit like asking whether you should eat ice cream or drink coffee. The two are as different as chalk and cheese.

If you can manage it, use final a lot on variables. It means a variable can be set up once and once only. After that it cannot be reassigned. It makes it easier to watch the values of variables. The final keyword can be applied both to fields and to local variables. It can also be applied to classes and methods, but that is something different.

The static keyword means the field (or method, or nested class) "belongs" to the class, not to an individual instance, and this is totally different. A static field is one you want to share between all instances of the class (or public static final = use as a constant). A static method is one which takes no information from a particular instance of the class and applies no information fro any particular instances.

I am sure other people will have their own opinions
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Thank you!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37907
    
  22
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static vs. Final
 
Similar Threads
Stateless Session Bean variables static or non-static.
alternative for interface
can main be declaired final ? why
static final array
final static int i =0 Whizlabs says i is variable and i feel it is a constant