Forums Register Login

Final in loop

+Pie Number of slices to send: Send
This looks strange to me. Why is the iteration-holder made final??

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Ah ok, makes sense.
Thanks.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

 
+Pie Number of slices to send: Send
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.
Not looking good. I think this might be the end. Wait! Is that a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2203 times.
Similar Threads
Combining HtmlUnit and HttpClient
JwebUnit issue
ADF MyFaces and HttpUnit testing
cannot get https page details
FaceBook API
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:17:31.