0
0
Kotlinprogramming~10 mins

Properties with val and var in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Properties with val and var
Declare property with val
Property is read-only
Try to reassign?
YesError: Cannot reassign val
No
Declare property with var
Property is mutable
Reassign allowed
Use property value
This flow shows how val creates read-only properties and var creates mutable properties that can be reassigned.
Execution Sample
Kotlin
val name = "Alice"
var age = 30
// name = "Bob" // Error
age = 31
println("$name is $age years old")
This code declares a read-only name and a mutable age, then changes age and prints both.
Execution Table
StepActionPropertyValue BeforeValue AfterOutput/Note
1Declare val namenameundefined"Alice"name set to "Alice"
2Declare var ageageundefined30age set to 30
3Attempt to reassign namename"Alice"ErrorError: Cannot reassign val property
4Reassign ageage3031age changed to 31
5Print statement---"Alice is 31 years old"
💡 Execution stops after print; reassignment to val causes error and is commented out.
Variable Tracker
PropertyStartAfter Step 1After Step 2After Step 3After Step 4Final
nameundefined"Alice""Alice"Error (reassign not allowed)"Alice""Alice"
ageundefinedundefined30303131
Key Moments - 2 Insights
Why can't we change the value of a property declared with val?
Because val properties are read-only after initialization, as shown in step 3 of the execution_table where reassignment causes an error.
Can we change the value of a property declared with var?
Yes, var properties are mutable and can be reassigned, as shown in step 4 where age changes from 30 to 31.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What happens when we try to reassign the val property 'name'?
AThe program ignores the reassignment
BThe value changes successfully
CAn error occurs because val properties cannot be reassigned
DThe value becomes null
💡 Hint
Check the 'Output/Note' column at step 3 in the execution_table.
According to variable_tracker, what is the value of 'age' after step 4?
A31
B30
Cundefined
DError
💡 Hint
Look at the 'age' row under 'After Step 4' in variable_tracker.
If we change 'val name' to 'var name', what would happen at step 3?
AAn error would still occur
BReassignment would succeed and name would change
CThe program would crash
DThe value would become null
💡 Hint
Recall that var properties are mutable as explained in the concept_flow and key_moments.
Concept Snapshot
val declares a read-only property (cannot be reassigned).
var declares a mutable property (can be reassigned).
Reassigning val causes a compile-time error.
Use val for constants and var for variables.
Access properties by name to get their current value.
Full Transcript
In Kotlin, properties declared with val are read-only and cannot be changed after they are set. Properties declared with var are mutable and can be reassigned. In the example, 'name' is declared with val and set to "Alice". Trying to change 'name' causes an error. 'age' is declared with var and initially set to 30, then changed to 31 successfully. The program prints the final values. This shows how val and var control mutability of properties.