Featured post
- Get link
- X
- Other Apps
Val cannot be reassigned compile time error in kotlin function solved
Hi friends,
I was writing a kotlin function.I used the keyword val as shown below
fun main() {
println("Hello, world!")
// Hello, world!
val subscribers = 500
subscribers=600
}
When i execute the above code,i got an error as shown below
Val cannot be reassigned
It means , when we run the line val subscribers = 500, the local variable subscribers is assigned with the value 500.Then in the next line when we execute, subscribers=600,we are trying to reassign the local variable subscribers with the value 600.
Actually In Kotlin val declares final, read only, reference. We can can not reassign such variables with new values.So we got the error message as
Val cannot be reassigned
How can we solve Val cannot be reassigned error in Kotlin
By using var instead of val in kotlin, we can resolve the error.Kindly check below example
fun main() {
println("Hello, world!")
// Hello, world!
var subscribers = 500
subscribers=600
}
Popular Posts
How to Check React Version Installed in Your System by Checking PACKAGE.JSON File and Executing Command from COMMAND PROMPT
- Get link
- X
- Other Apps
Execution failed for task ':react-native-reanimated:externalNativeBuildDebug' solved
- Get link
- X
- Other Apps
Error: Invalid `prisma.$executeRaw()` invocation: fixed using log: ["query"] option
- Get link
- X
- Other Apps
app keeps stopping error solved in android studio emulator using logcat in react native project
- Get link
- X
- Other Apps
ERROR Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined
- Get link
- X
- Other Apps