It's not a secret anymore!
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 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
Author

What ARE appropriate uses of final and static

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

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
Bartender

Joined: Oct 02, 2003
Posts: 11882
    
  18

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
Author
Rancher

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: 15002
    
  27

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 8 API documentation
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138

Hi,

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

Thanks,

Rahul




Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44585
    
  34
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 ... ...


BEE MBA PMP SCJP-6
Thanigai arasu Ruthirakotti
Greenhorn

Joined: Jan 24, 2015
Posts: 3
The final is the powerful keywords in java for class design.

While design your application, The class that you don't want to inherit then define as the final class. Mostly the Util classes should be the final class. String is one of the final class in java. If you want the method should not be over-ridden, then the method should be final. If you want to declare the variable to treat like constant, then it should be final variable.

Static keyword is used for class level implementation of Variables/Methods and not for Object level. The intention here is to access the variable using the directly the Class Name. Same time, if you mentioned object.methodName() or object.variableName for calling the static method then also internally it will consider it as ClassName.methodeName().
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 484

Ha, it is funny to see a reply notification email show up for something I posted 4 years ago. I have been a developer for 4 1/2 years now, and I definitely know the uses of final and static at this point.
It is kind of funny, and actually pretty embarrassing to look back and see my past self ask these types of questions now
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44585
    
  34
We were all at that stage once, so there is no need to be embarrassed.
Liutauras Vilda
Bartender

Joined: Nov 12, 2014
Posts: 787
    
  16

Matt Kohanek wrote:Ha, it is funny to see a reply notification email show up for something I posted 4 years ago. I have been a developer for 4 1/2 years now, and I definitely know the uses of final and static at this point.
It is kind of funny, and actually pretty embarrassing to look back and see my past self ask these types of questions now


In order to stop notifications about the post you're not interested anymore, press button "Stop Watching".

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2738
    
  56
Matt Kohanek wrote:Ha, it is funny to see a reply notification email show up for something I posted 4 years ago. I have been a developer for 4 1/2 years now, and I definitely know the uses of final and static at this point.
It is kind of funny, and actually pretty embarrassing to look back and see my past self ask these types of questions now

The other way to look at it is wow haven't I learn't a lot in the last 4 years.
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1359
    
  19

David Newton wrote:Finally, variables must be final if they're going to be used by an anonymous inner class.

Beginning with Java 8 this statement is no longer true. ;)


OCPJP 6, 7, 8, OCMJD 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What ARE appropriate uses of final and static
 
It's not a secret anymore!