Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I was told that, I misunderstand effects of `final`. What are the effects of `final` keyword?
  2.  
  3. Here is short overview of what I think, I know:
  4. > ###Java final modifier (aka aggregation relation)
  5. >
  6. > **primitive variables**: can be set only once. (memory and performance
  7. > gain)<br>
  8. > **objects variables**: may be modified, final applies to object
  9. > reference.<br>
  10. > **fields**: can be set only once.<br>
  11. > **methods**: can't be overridden, hidden.<br>
  12. > **classes**: can't be extended.<br>
  13. > **garbage collection**: will force Java generational garbage collection
  14. > mark-sweep to double sweep.
  15.  
  16. ###Can's and Cant's
  17. - Can make clone fail (this is both good and bad)
  18. - Can make immutable primitives aka const
  19. - Can make blank immutable - initialized at creation aka readonly
  20. - Can make objects shallowly immutable
  21. - Can make scope / visibility immutable
  22. - Can make method invocation overhead smaller (because it does not need virtual table)
  23. - Can make method arguments used as final (even if thy are not)
  24. - Can make objects threadsafe (if object is defined as final, it wont make method arguments final)
  25. - Can make mock tests (not that you could do anything about it - you can say bugs are intended)
  26. - Can't make friends (mutable with other friends and immutable for rest)
  27. - Can't make mutable that is changed to be immutable later (but can with factory pattern like fix)
  28. - Can't make array elements immutable aka deeply immutable
  29. - Can't make new instances of object (this is both good and bad)
  30. - Can't make serialization work
  31.  
  32. There are no alternatives to `final`, but there is wrapper + private and enums.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement