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
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
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.
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:
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...
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.