This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When to use "final"?  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I am new to Java but learning through classes and at work. At work I have been given small implementation work to help me learn. I noticed that in similar class files some developers use "final" where others do not. I think I understand the use of final is when the variable is meant to not be changed. In both examples I'm looking at the variables are just that. They aren't supposed to change. So I take it the developer who did use final is more technically correct?

Thank You!
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • final is keyword used as non- access modifier applicable for classes, methods and variables.

  • final class:
  • If a class declared as final then we can’t extend that class. i.e. we can’t create child class of that class.
  • The methods of a final class are never overridden.

  • final method:
  • If a method is declared as final then that final method cannot be overridden in a subclass.
  • A method can be declared final to prevent subclasses from overriding or hiding it.
  • It is a compile-time error to attempt to override or hide a final method.
  • A private method and all methods declared immediately within a final class behave as if they are final, since it is impossible to override them.

  • final variable:
  • If a variable declared as final then it will become constant and we can’t perform re-assignment for that variable.
  • Once a final variable has been assigned, it always contains the same value.
  • If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.
  • This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.
  •  
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16028
    87
    Android IntelliJ IDE Java Scala Spring
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish explained exactly what final means. It can indeed be used on classes, methods and variables and has slightly different meanings for all those cases.

    Whether you should use final is also partly a matter of style.

    Some people are of the opinion that you should for example make classes final unless they are designed to be subclassed. Likewise with methods - you might decide to make them final unless they are designed to be overridden in subclasses. And also with variables - when a variable is final it can't be modified, and this can make programs "safer" - you can't accidentally modify such a variable anywhere (if you try the compiler will give you an error).

    It is often a good idea to make classes immutable, especially in multi-threaded programs. Writing multi-threaded programs is hard, and what especially makes it hard is if you have multiple threads that may modify the same data at the same time - you can get really difficult bugs because of the unpredictability of the order in which things happen exactly if you have two threads trying to write to the same variable. Making things immutable is one way to make multi-threaded programming easier. Making member variables final is part of what is necessary to make classes immutable, so that might be another reason why people are making variables final.
     
    Campbell Ritchie
    Marshal
    Posts: 55707
    163
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish Patil wrote:. . . final variable:
  • If a variable declared as final then it will become constant and we can’t perform re-assignment for that variable.
  • Once a final variable has been assigned, it always contains the same value.
  • . . .
    I think that needs a bit more explanation.

    A final field or local variable must be assigned to once and once only. But if the assignment is to a mutable reference type, the state of that object can be changed. That applies to arrays, since arrays are always mutable reference types.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I think that needs a bit more explanation.
    yes I always fumble while explaining things & constructing english statements .
    Campbell Ritchie wrote:A final field or local variable must be assigned to once and once only
    This only one line which is short and precise substitutes mine above two line.
     
    Quazi Irfan
    Ranch Hand
    Posts: 104
    1
    Java Netbeans IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:A final field or local variable must be assigned to once and once only. But if the assignment is to a mutable reference type, the state of that object can be changed. That applies to arrays, since arrays are always mutable reference types.


    Do you mean we can use the reference to change the it's instance variables, but we can't point the reference to a new object?
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Likes 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Quazi Irfan wrote:Do you mean we can use the reference to change the it's instance variables, but we can't point the reference to a new object?
    Yes, we can use that reference to change the values of instance variables if those are not declared final but we can't point to new object. Here I wrote a program so you can understand it. Output:
    My age: 29
    My age: 25
     
    Quazi Irfan
    Ranch Hand
    Posts: 104
    1
    Java Netbeans IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you
     
    Liutauras Vilda
    Marshal
    Posts: 4640
    316
    BSD
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ganish Patil wrote:Here I wrote a program so you can understand it.
    You got weird sequence for your class members. Declaration should follow next sequence:
    1. Class
    2. Instance variables
    3. Constructor/-s
    4. Methods

    Somehow you flipped 3 with 4, which I don't think is good.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Quazi Irfan wrote:Thank you
    Your welcome
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:Somehow you flipped 3 with 4, which I don't think is good.
    Opps No I always follow sequence you mentioned. Didn''t notice that, Got auto getter setter methods so preceded constructor. But Thank you for noticing that
     
    Campbell Ritchie
    Marshal
    Posts: 55707
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The order of members of a class is a style convention; the compiler won't even notice the methods are out of order.
     
    Lisa Austin
    Ranch Hand
    Posts: 178
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper de Jong wrote:Ganish explained exactly what final means. It can indeed be used on classes, methods and variables and has slightly different meanings for all those cases.

    Whether you should use final is also partly a matter of style.

    Some people are of the opinion that you should for example make classes final unless they are designed to be subclassed. Likewise with methods - you might decide to make them final unless they are designed to be overridden in subclasses. And also with variables - when a variable is final it can't be modified, and this can make programs "safer" - you can't accidentally modify such a variable anywhere (if you try the compiler will give you an error).

    It is often a good idea to make classes immutable, especially in multi-threaded programs. Writing multi-threaded programs is hard, and what especially makes it hard is if you have multiple threads that may modify the same data at the same time - you can get really difficult bugs because of the unpredictability of the order in which things happen exactly if you have two threads trying to write to the same variable. Making things immutable is one way to make multi-threaded programming easier. Making member variables final is part of what is necessary to make classes immutable, so that might be another reason why people are making variables final.


    Thank You! This is pretty much what I thought and helps. I know what the "definition" ( I guess it would be called ) for final but I was looking more for the I guess "style" to when some people use it.
     
    Lisa Austin
    Ranch Hand
    Posts: 178
    3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Ganish Patil wrote:. . . final variable:
  • If a variable declared as final then it will become constant and we can’t perform re-assignment for that variable.
  • Once a final variable has been assigned, it always contains the same value.
  • . . .
    I think that needs a bit more explanation.

    A final field or local variable must be assigned to once and once only. But if the assignment is to a mutable reference type, the state of that object can be changed. That applies to arrays, since arrays are always mutable reference types.


    Ah good to know. Thank You. I appreciate the clarification.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:The order of members of a class is a style convention
    I think, Liutauras Vilda also meant the same
     
    Joe Bishara
    Ranch Hand
    Posts: 175
    17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Lisa Austin wrote:...but I was looking more for the I guess "style" to when some people use it.

    I'm assuming that you want to know when it is okay to use the final keyword. Use the final keyword when an implementation is final i.e. when an improved or alternative version of an implementation is not allowed. When you extend a class or override a method, you are providing an improved or alternative version of the class or method. According to the Liskov substitution principle, a subtype is a specialized form of its supertype i.e. a subtype is an equal or even better substitute for its supertype, for example anything a JTextField can do, a JFormattedTextField can do equally or even better. However, what happens if a class or method should never be improved on? What happens if you should never provide an alternative version? You make it final. For example, some methods in the Object class (like wait() and notify()) are final because we must never provide an improved or alternative version. The Math, String and Integer classes are final for the same reason.
     
    Campbell Ritchie
    Marshal
    Posts: 55707
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also, if you make something final which shouldn't be final, and you get problems later, you can always remove the final tag. You cannot necessairily add it. This leads us on to what Winston Gutkowski suggests: make everything in sight private and final and change those modifiers only if there are problems.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote: make everything in sight private and final and change those modifiers only if there are problems.
    Sounds like good idea, will definitely try it.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!