0

I have seen the link before asking question here, I wonder why SONAR complaining only arrays and not custom objects.

Because same problem can occur in the custom objects we declare in the bean as well. And also, i couldnt agree why we need to take copy as we like to modify the same array that's why we have setters and all correct?

Kindly help me to understand, SONAR is showing more number of violations in this category.

Community
  • 1
  • 1
Manoj
  • 5,707
  • 19
  • 56
  • 86

1 Answers1

1

Sharing mutable state should be avoided since it can cause error in your program. Especially if you are working in multi threaded environment. Also it can make you program much less readable. That's why communication throw modifying same state should be at least minimize.

Coping collections and arrays as well as dates are common practice to make sure that you don't use in your class/thread same object that was provided from other object/thread.

Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
  • Why it is not complaining object and only collections like arrays? – Manoj Jun 04 '14 at 16:23
  • Because for arrays (and java.until.Date objects), the parser knows for sure that they are mutable. For arbitrary objects, it is considered best practice to stick to immutable objects. – Mithfindel Jun 04 '14 at 18:47