• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When to use "final"?

 
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
  •  
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Marshal
    Posts: 79151
    377
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 146
    1
    IntelliJ IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Eclipse IDE MySQL Database Tomcat Server Java
    • Likes 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 146
    1
    IntelliJ IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you
     
    Marshal
    Posts: 8856
    637
    Mac OS X VI Editor BSD Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Quazi Irfan wrote:Thank you

    Your welcome
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 333
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 333
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Ranch Hand
    Posts: 175
    17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
    reply
      Bookmark Topic Watch Topic
    • New Topic