aspose file tools*
The moose likes Beginning Java and the fly likes Final in loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Final in loop" Watch "Final in loop" New topic
Author

Final in loop

Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
This looks strange to me. Why is the iteration-holder made final??

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



ASCII silly question, Get a silly ANSI.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Ah ok, makes sense.
Thanks.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

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

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
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
 
subject: Final in loop
 
Similar Threads
JwebUnit issue
FaceBook API
cannot get https page details
ADF MyFaces and HttpUnit testing
Combining HtmlUnit and HttpClient