Local VS global variables in Java

后端 未结 2 880
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-18 15:53

I thought I understood the difference between local and global variables in java until I saw an example today. In this code one tries to add elements to a link list in a met

2条回答
  •  情书的邮戳
    2020-12-18 16:16

    enter image description herenewElement is just a reference to an object created in memory.

    firstElement then holds a reference to the same object.

    The reference newElement is indeed a local variable, but the object referred to by the reference is then also referred to by another reference, i.e. firstElement. So after the addDataPacket() method completes, the newElement reference no longer exists, but the object that it referred to still exists in memory and that object is referred to by firstElement.

提交回复
热议问题