Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Help with final keyword

 
Greenhorn
Posts: 10
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I know the use of variable,class, and methods with the final keyword but not when creating object.

Im creating a JFrame and i added JComboBox to it, and then i saw a example of someone who added the final keyword
at the beginning of it like this:

final JComboBox cb = new JComboBox(languages);  

So my question is, what is the use of it?
 
Marshal
Posts: 74354
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

The final keyword isn't specific to Swing, but there are several reasons why one might declare a variable final. One is as a simple precept of good programming, to prevent it being reassigned later. It might be required because it is a local variable or a parameter used in an anonymous class, but we would need to know more details before we can be certain.
 
Ranch Hand
Posts: 33
Android Netbeans IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because of the final keyword the value of this variable can't change after assignment. In this case the value is a reference to an object. So the variable always refers to that object but the values of the object variables can still change.
 
Sigge Pigge
Greenhorn
Posts: 10
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks for the help!
 
Saloon Keeper
Posts: 13379
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's another very subtle but important consequence of making an instance field final. It acts as a memory fence when you access it after constructing an object. Consider this program:

What are the possible outputs of this program? It can be "Before reassignment" if the call to println() happens before the call to setItem(), or it can be "After reassignment" if the new thread manages to call setItem() first.

There's actually a third outcome. It's possible that this application prints "null". This happens because threads are allowed to reorder operations as long as the reordering of those operations is not visible to the code that thread is executing. However, the reordering of those operations may still be visible to other threads. Consider the operations that the new thread performs:

  • Create a new Container.
  • Assign the string "After reassignment" to its instance field.
  • Pass the Container reference to the setItem() method call.

  • However, without synchronization, the main thread may see the following order of operations:

  • Create a new Container.
  • Pass the Container reference to the setItem() method call.
  • Assign the string "After reassignment" to its instance field.

  • This means that the main thread may call println() after the new Container has been passed to the setItem() method call, but before the instance field has been assigned (and so has the default value null).

    Before Java 5 you had to fix this using a bunch of ugly synchronized blocks, but since Java 5 final fields are guaranteed to have the value you initialized them with when control returns from a constructor call.
     
    Bartender
    Posts: 561
    11
    IntelliJ IDE Spring Fedora
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    How come you are able to reset the item if the class is final?  Doesn't that make the variable final, too?
     
    Marshal
    Posts: 26910
    82
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Al Hobbs wrote:How come you are able to reset the item if the class is final?  Doesn't that make the variable final, too?



    No, it doesn't. A final class can't be extended, that's what "final" means there.
     
    Campbell Ritchie
    Marshal
    Posts: 74354
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Clapham wrote:. . . No, it doesn't. A final class can't be extended . . . .

    Another way to put what Paul said is that the final modifier would be applied to the class, not to the fields.

    You can apply final to classes, to methods, and to fields, and it means something different in each location.
     
    Ranch Hand
    Posts: 1067
    2
    IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Off topic, sort of.

    Question from an interview.  "What is final, finally and finalize?"
     
    Campbell Ritchie
    Marshal
    Posts: 74354
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    For a start, final and finally still exist.

    The finalize() method is now deprecated.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic