Home  >  Q&A  >  body text

java中final修饰的变量不是不能被修改吗?为什么可以add添加?

怪我咯怪我咯2719 days ago669

reply all(4)I'll reply

  • 黄舟

    黄舟2017-04-18 10:57:12

    The searchableList variable itself cannot be modified, but the object pointed to by searchableList can be modified.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:57:12

    The usage of this final is final variable, which means that the reference of the searchableList variable cannot be changed and has been locked on that ArrayList, but the ArrayList itself can be changed.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:57:12

    Final modifies the heap memory space pointed to by the searchableList variable. For example, searchableList is your house. Being modified by final means that the house will not change anymore, but the content inside can be changed. You can also add things to the house. Go in or delete updates, etc.

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:57:12

    The final modified searchableList is a reference, and this reference points to ArrayList<String>, so searchableList cannot be modified, and ArrayList is implemented with an array, and new elements can be added to this array.
    When you try to modify the object ed pointed to by searchableList, an error is reported

    reply
    0
  • Cancelreply