aspose file tools*
The moose likes Beginning Java and the fly likes What ARE appropriate uses of final and static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What ARE appropriate uses of final and static" Watch "What ARE appropriate uses of final and static" New topic

What ARE appropriate uses of final and static

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

In school and in any subsequent tutorials I have read through, the instances in which you should not use final and static are given. However, I have never really seen or been given an example of when using these keywords is appropriate. In all my programs I have never really needed them either.

Can anyone give me a real example of when they are useful and/or necessary?

True wisdom is in knowing you know nothing - Socrates
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 10925

You can use 'final' when you don't want the value of a variable to change once it is set.

Static is used when you want to be able to reference the variable without creating an instance of the class.

for example, in the Math class, there is a variable called PI. It is final, because you don't want anyone to be able to change it. It is static, because there is no reason to force someone to create an instance of the Math class just to get the value - the value can exist regardless.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Newton

Joined: Sep 29, 2008
Posts: 12617

The final keyword can also be used to express intent. Some people declare all parameters as final to avoid accidental (or purposeful) reuse in a method body. Some people declare some or all local variables as final, again to express intent.

Finally, variables must be final if they're going to be used by an anonymous inner class.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884

It's a good idea to make classes final unless they are designed to be extended by subclasses: see Item 17 ("Design and document for inheritance or else prohibit it") in the book Effective Java (highly recommended if you want to become a better Java programmer!).

Also, I prefer to make classes immutable because it has a number of advantages (you don't need to make defensive copies, they are thread-safe etc.). For example, instead of writing a simple class like this:

I prefer to write it like this:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138


The Final keyword is one of the useful keyword in class design. Some classes in java language is marked final e.g. String, Wrapper Classes, StringBuffer, etc. Obviously, sometimes there is no need to extend classes in our application. e.g. String in java provide some machine level implementation(String memory pool) for creating objects of string, so in that case user don't need to extend the class. And Class Designer also don't want it so... They have marked class as final.

Static keyword is used for class level implementation of Variables/Methods and not for Object level. So for this purpose we can access the variable using the directly the Class Name and not an object of the class, Though you have mentioned "object.methodName()" for calling the static method then also internally it will consider it as "ClassName.methodeName()". As obvious it will also get rid of unnecessary objects that we have to create for calling the methods which we can mark as static.

Ranchers please correct me if i am wrong somewhere...



Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36579
The final keyword has different effects depending on whether it is applied to a class, a method or a field/variable.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Your question about the appropriate uses of 'static' and 'final' is a little like
asking about the appropriate uses of a hammer or a shovel. 'static' and 'final'
are very powerful general purpose Java tools. So beyond saying, "Use them to
pound on something, or to dig at something," trying to list (and memorize) good
uses can be an endless task.

A better approach is to gain a full understanding of these Java concepts, in part by
studying examples, of course. You will then be able to recognize design situations
where 'static' and 'final' should be applied.

Jim ... ...

I agree. Here's the link:
subject: What ARE appropriate uses of final and static
Similar Threads
Doubt in Static initialization
Lack of elegance in Java
Static vs. Final
Hashmap, retrieving value and re-storing with different key
Welcome Kathy Sierra and Bert Bates to our Book Promotion of the Week