• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

What ARE appropriate uses of final and static

 
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
lowercase baba
Posts: 12760
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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




 
Marshal
Posts: 65398
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The final keyword has different effects depending on whether it is applied to a class, a method or a field/variable.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 65398
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We were all at that stage once, so there is no need to be embarrassed.
 
Marshal
Posts: 7079
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 2220
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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. ;)
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!