5

I have enabled Checkstyle for my project. It is showing yellow mark on method parameters and requesting to set it as final. Why? What is the purpose? If not specified what will be the problem?

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
Jothi
  • 14,720
  • 22
  • 68
  • 93
  • 1
    Little details like the language and platform you are using would be handy. – Oded Sep 03 '10 at 12:50
  • 1
    Tools like Checkstyle should be considered suggestions or starting points for building out what your team considers good form. I have seen teams waste a lot of time answering every perceived defect from static analysis tools without putting any real human thought into it. That being said @Balazs has a good answer for the general "why you should do this" part of the question. – cjstehno Apr 15 '15 at 18:19

2 Answers2

4

In case there is a rule in Checkstyle, normally the answer is there as well: http://checkstyle.sourceforge.net/config_misc.html

In this case:

Changing the value of parameters during the execution of the method's algorithm can be confusing and should be avoided. A great way to let the Java compiler prevent this coding style is to declare parameters final.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
  • 1
    When marking method params final, aren't they copied to the object for serialization purposes? – Jan Krakora May 16 '13 at 09:47
  • 1
    Method parameters are like local variables. They have nothing to do with object serialization. Or I misunderstood your question. – Balazs Zsoldos May 16 '13 at 09:51
  • Yes, but if you want to use local variable in an anonymous class defined in the method, you have to mark it final. This copies the variable to the object (to be accessible from the anonymous class) and as a side effect it is sertialized with the object. – Jan Krakora May 16 '13 at 10:20
  • You are right, only constant variables can be used in anonymous classes. But I think that question does not belong to here as this topic is more about code formatting and coding conventions. The topic you are looking for is http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class – Balazs Zsoldos May 16 '13 at 11:28
  • Well, I think it does. If final marked params are always (that's what I'm not sure) copied to the enclosing object, then marking them final is not good coding convention - because you include them in serialized form unnecessary. – Jan Krakora May 16 '13 at 11:39
  • 1
    Are you sure any serialization is done? In case there is an anonymous class as many variables are created as many variables are used from outside. They are passed via a hidden constructor. Only pointers are passed, there is no serialization or deserialization. Not all final variables are passed but only the ones that are used within the anonymous class. You can try it with a simpla app that has two AtomicInteger variables and you use only one of them inside the anonymous class. In the debugger you will see that the not-used variable is not seen as a member variable. – Balazs Zsoldos May 16 '13 at 13:22
  • @BalazsZsoldos What do you mean "Only pointers are passed"? Java has no pointers. – IgorGanapolsky Nov 07 '13 at 18:59
  • I meant that the memory address of the object is passed. Pointer is not a good word here but I cannot edit my comment as it was too long time ago. – Balazs Zsoldos Nov 07 '13 at 19:35
1

Remove FinalParameters from your CheckStyle declaration. http://checkstyle.sourceforge.net/config_misc.html#FinalParameters