0
0
Blockchain / Solidityprogramming~10 mins

Reference types behavior in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reference types behavior
Create Object A
Assign Ref1 = A
Assign Ref2 = Ref1
Modify via Ref2
Check Ref1 shows change
Reassign Ref2 to new Object B
Ref1 still points to A
End
This flow shows how reference types point to the same object, so changes via one reference affect the other, but reassigning a reference breaks the link.
Execution Sample
Blockchain / Solidity
objectA = {"value": 10}
ref1 = objectA
ref2 = ref1
ref2["value"] = 20
ref2 = {"value": 30}
print(ref1["value"])
This code shows two references to the same object, modifying it through one reference, then reassigning the second reference.
Execution Table
StepActionref1ref2objectAOutput
1Create objectA with value 10nullnull{"value":10}
2Assign ref1 = objectA{"value":10}null{"value":10}
3Assign ref2 = ref1{"value":10}{"value":10}{"value":10}
4Modify ref2["value"] = 20{"value":20}{"value":20}{"value":20}
5Reassign ref2 = {"value":30}{"value":20}{"value":30}{"value":20}
6Print ref1["value"]{"value":20}{"value":30}{"value":20}20
💡 Execution ends after printing ref1["value"] which is 20, showing ref1 still points to original object.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
objectAnull{"value":10}{"value":10}{"value":20}{"value":20}{"value":20}
ref1null{"value":10}{"value":10}{"value":20}{"value":20}{"value":20}
ref2nullnull{"value":10}{"value":20}{"value":30}{"value":30}
Key Moments - 2 Insights
Why does changing ref2["value"] also change ref1["value"]?
Because ref1 and ref2 both point to the same object (objectA) at that time, so modifying via one reference affects the shared object, as shown in step 4 of the execution_table.
Why does ref1["value"] stay 20 after ref2 is reassigned?
Reassigning ref2 to a new object breaks its link to objectA, but ref1 still points to the original objectA, so ref1["value"] remains 20, as seen in step 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of ref1["value"]?
A10
B20
C30
Dnull
💡 Hint
Check the 'ref1' column at step 4 in the execution_table.
At which step does ref2 stop pointing to the same object as ref1?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look at when ref2 value changes independently in the execution_table.
If we changed ref2["value"] = 50 at step 4 instead of 20, what would ref1["value"] be at step 6?
A20
B30
C50
D10
💡 Hint
Since ref1 and ref2 share the same object before step 5, changes via ref2 affect ref1.
Concept Snapshot
Reference types store addresses to objects.
Multiple references can point to the same object.
Changing the object via one reference affects all references.
Reassigning a reference breaks the link to the original object.
Useful to understand shared state in blockchain data structures.
Full Transcript
This lesson shows how reference types behave in blockchain programming. We start by creating an object with a value. Then we assign two references to this same object. When we change the value through one reference, the other reference sees the change because they point to the same object. Later, when we assign a new object to one reference, it no longer points to the original object, but the other reference still does. This explains why changes through one reference affect others until reassignment breaks the link. Understanding this helps avoid bugs when working with shared data in blockchain code.