0

I am using com.sun.codemodel to generate some java entity objects

i want to generate a for loop as follows:-

for (final Field field : classFields) {}

However i can only manage this

for (Field field : classFields) {}

is it possible to add final within a ForEach generated statement?

Hector
  • 4,016
  • 21
  • 112
  • 211

1 Answers1

1

It is not necessary to add the final modifier here, because the foreach-loop in Java is just syntactic sugar. Internally the Java Iterator is used. Due to this you can not assign another Field Object to the variable of the foreach-loop, which makes it already final. You can only modify it's interal state via it's setter methods. For mor details this might be helpful.

Westranger
  • 1,308
  • 19
  • 29
  • correct, i dont HAVE to use final modifier, however i would like to... and codeModle doesnt seem to allow it – Hector Sep 09 '14 at 10:37
  • it makes no sense to use it and this is (in my opinion) the reason why the compiler and the codeModel does not allow you to use it – Westranger Sep 09 '14 at 11:13