Are there any reasons to make all fields and variables final?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-03-27T18:44:01Z Indexed on 2010/03/27 18:53 UTC
Read the original article Hit count: 263

Filed under:
|
|

In my current project I noticed that all class fields and variable inside methods are declared with final modifier whenever it's possible.

Just like here:

private final XMLStreamWriter _xmlStreamWriter;
private final Marshaller _marshaller;
private final OutputStream _documentStream;

private final OutputStream _stylesStream;
private final XMLStreamWriter _stylesStreamWriter;
private final StyleMerger _styleMerger;

public DocumentWriter(PhysicalPackage physicalPackage) throws IOException {
    final Package pkg = new Package(physicalPackage);

    final Part wordDocumentPart = pkg.createPart(
            "/word/document.xml",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");

    // styles.xml
    final Pair<Part, String> wordStylesPart = wordDocumentPart.createRelatedPart(...);
    ...
}

Are there any reasons to do so?

p.s. As I know project is not supposed to be multithreaded (at least I've heard nothing about it).

© Stack Overflow or respective owner

Related posts about java

Related posts about coding-style