immutable objects and lazy initialization.

后端 未结 4 1808
悲哀的现实
悲哀的现实 2021-01-20 01:31

http://www.javapractices.com/topic/TopicAction.do?Id=29

Above is the article which i am looking at. Immutable objects greatly simplify your program, since they:

4条回答
  •  盖世英雄少女心
    2021-01-20 01:40

    And to add to other answers.

    Immutable object cannot be changed. The final keyword works for basic data types such as int. But for custom objects it doesn't mean that - it has to be done internally in your implementation:

    The following code would result in a compilation error, because you are trying to change a final reference/pointer to an object.

    final MyClass m = new MyClass();
    m = new MyClass();
    

    However this code would work.

    final MyClass m = new MyClass();
    m.changeX();
    

提交回复
热议问题