• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final in loop

 
Stuart Ash
Ranch Hand
Posts: 637
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks strange to me. Why is the iteration-holder made final??

Source: http://htmlunit.sourceforge.net/table-howto.html

 
Scott Selikoff
author
Saloon Keeper
Posts: 3901
16
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its not that odd, since the final reference is thrown away after each iteration of the loop, it ensures that the loop reference is not changed during its execution.

The argument for marking that variable final is similiar to having any local variables marked final such as declaring variables at the top of methods with final references. Often times it makes sense to if you know the data (or reference to the data) is read only during the excution.
 
Stuart Ash
Ranch Hand
Posts: 637
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok, makes sense.
Thanks.
 
Scott Selikoff
author
Saloon Keeper
Posts: 3901
16
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ever since taking the SCJP test, I tend to use final as much as possible in my code. I've found in the majority of cases (especially with objects) it makes sense to use final and its also helped me find bugs, since most IDEs warn you if you set final variables multiple times.

At the very least, it gives you some added sanity in debugging problems.

On an unrelated note about temporary loop variables... One trick I learned in performance enhancements in loops is if you can reuse objects without call 'new', you will generally have some performance improvement. Granted, in the example you provided, this isn't really applicable, but in other simple objects like VOs it can be preferable to re-use the same object and set new values for it. It can also lead to side effects if you're not careful, so use with caution.
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a ContractualJ imperative to approach valid software requirements.
Part of this means restricting scope to all that is necessary, with no excess.
For example, it exceeds requirement to have a local "variable" that is only assigned once (per requirements), but permit a reassignment further on. This means it must be declared final. Local finals are not "write once", but "write zero or once", as long as they are assigned through definite assignment semantics upon their first use. Final fields are always write once. For the record, and since I observe this confusion often, this (a final) is different to a constant.


# All method parameters are declared final.
# All local variables and fields are declared final, unless otherwise required not to be by the context. A local variable or field that is assigned once must be declared final.
 
Stuart Ash
Ranch Hand
Posts: 637
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Useful comments, thanks guys.

Originally posted by Scott Selikoff:

On an unrelated note about temporary loop variables... One trick I learned in performance enhancements in loops is if you can reuse objects without call 'new', you will generally have some performance improvement. Granted, in the example you provided, this isn't really applicable, but in other simple objects like VOs it can be preferable to re-use the same object and set new values for it. It can also lead to side effects if you're not careful, so use with caution.


Agree, but it is the risk of forgetting to reset something which would often lead one to create an object anew.

With classes such as VOs, it might be good to provide a reset() method which can be tested to be bug-free and used without the unwanted side effects.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic