I thought I could understand the difference between the local and global variable in Java until that time no example was seen . This code tries to add elements in a linked list in a method:
public void addDataPacket (data packet data) {PacketQueueElement newElement = new PacketQueueElement (data); If (first element! = Null) {lastElement.setNextElement (newElement); Previous element = new element; } Else {firstElement = newElement; Previous element = new element; }}
I do not understand why the new aliment disappeared after the method closed? Because it is a local variable and is not defined anywhere in the classroom. Here is the complete code for this class:
Public class packet quiz {/ ** Das Istix agencies / private packet quotable first element in Des Whartclanche; / ** Das Lazte Element in Der Wirtschange * / Private PacketQuillage Ultimate Element; / ** * Instructions for the Warteschlange * / Public Packet Question () {this.firstElement = null; This.lastElement = null; } / ** * Fyugt AIN news pocket ins Ander Vorteslenj A * @param data Slave New packets * / public void Jodendeta packet data (packet data) {PacketQueueElement newElement = new PacketQueueElement (data); If (first element! = Null) {lastElement.setNextElement (newElement); Previous element = new element; } Else {firstElement = newElement; Previous element = new element; }} / ** * Antfrnt slave Arstiks element fear Vortschenj and Gebert's Jhurek * @ Raton slave Ahiment these fears Wartschlaj * / public packet Kyuieliment down and Rmvefrst Element () {packets Kyuieliment element = this.first Element; This.first element = element.getNextElement (); Return element; } / ** * Gift slave Strit pocket Aus dem Istrenn element Jhruek * @ Rurn slave Erie ripe * / public data packet Bring Frstdata packet () {this.first Element. GetData (); } / ** * Anton Das Arte Pocket der Wartchenenjen and Gibert S Zurek * @ Rattan Das first pocket these fearsalesnage * / public data packet and endorphic data packet () {come here. And AndroFirst Element () GetData (); } / ** * Element der Vertschillen Zurooq * * @Ruran Das Erstics Element * / Public Packet Qualitative Lesser Element () {this return to this. First element; } / ** * Uberproft, Ob Die Wirtschange Lear It * * @ Return true, Wen Hsieh cleared ITT * / public boolean Apti () (if (First Element null ==) {return true;} other {return false;}} / * (non-Jvadok) * @see java. lang.Object # toString () * / public string to string () {PacketQueueElement element = this.firstElement; string s = ""; while (element! = nULL) {s + = element + "\ n"; element = element .getNextElement ();} Return S;}
}
Thank you in advance
This is mixing variable and objects , newElement
There is a local variable in it and it is lost after the method is over, but the context indicates a object is suitable for an object garbage collection if no reference (variables) does not indicate it In this case, temporarily, both the newElement
and first element
were told. newElement
was lost after the method ended, but < Code> first element still indicates it, as The
Or in other words: A variable refers to an object, this object is not the same.
An analogy:
- Variable: piece of paper You
- Object: Home
- Garbage Collector: Demolition crew
I am building a house and writing it on scrap. You can get the paper there, I pass you the scrap of paper, you write in the address book from the scrap of paper, you throw the paper scrap.
Demolition examines the crew whether anyone is still watching if someone still keeps his address, then he is using a house. Even if you threw the scrap of paper, even if you know the address book, the house is still being used and it has not been demolished.
Comments
Post a Comment