| 175 | |
| 176 | = Global variables = |
| 177 | |
| 178 | Do NOT use global variables as a way to pass arguments between non-public methods. |
| 179 | This is an unclear method, potentially very dangerous - a lot of mistakes can be done using it. |
| 180 | |
| 181 | = Use final = |
| 182 | Usage of final modifier for classes, methods,and especially class members and method variables is a very good practise and prevents from a number of mistakes. |
| 183 | |
| 184 | == classes == |
| 185 | When writing a class, one of the things we have to think of is - 'is our class designed in a way that it can be inherited?' |
| 186 | In 90% of the cases the answer of this question is 'NO'. However if we don't make it final, some people may decide to inherit it and this will cause a lot of problems. |
| 187 | There are some very famous examples of such mistakes being made: |
| 188 | java.sql.Date inherits java.util.Date and it causes a lot of problems - You can read about them here: |
| 189 | [http://www.thunderguy.com/semicolon/2003/08/14/java-sql-date-is-not-a-real-date/] |
| 190 | The problem all starts from the fact that in the documentation of java.util.Date it is written that the class should not be inherited,but it was not declared final. |
| 191 | Always put final modifier for your classes if they are not designed to be inherited. |
| 192 | |
| 193 | == class methods == |
| 194 | Sometimes we design our classes for inheritance but there are some public methods that are critical and we do not want the inheritant to override them - again put final modifier. |
| 195 | |
| 196 | == class variables == |
| 197 | When we have a class member that is initialized in the constructor and should never be changed through all the instance life - ALWAYS put final modifier. |
| 198 | This will prevent someone change the refference of that class member, that would have lead to wrong behaviour. |
| 199 | |
| 200 | In !FrameView class, we have !PageView parent, that was has to be final but it is not. |
| 201 | {{{ |
| 202 | private PageView parent; |
| 203 | }}} |
| 204 | Replaced with: |
| 205 | {{{ |
| 206 | private final PageView parent; |
| 207 | }}} |
| 208 | == method variables == |
| 209 | The same as class members. |
| 210 | {{{ |
| 211 | public void saveAs() { |
| 212 | File file = selectSaveFile(); |
| 213 | if(file != null){ |
| 214 | this.book.saveTo(file); |
| 215 | } |
| 216 | } |
| 217 | }}} |
| 218 | file shall be declared private. |