i would liek to run the simple example mentioned below. eclipse generates an error says:
main class cant be found or loaded
please let me know how to fix this error and why it happens in the below code I am trying to use the backing fields. however, the way they are used in the code does not provide the expected output. please refer to the output section.
how to display output of the backing fields
code:
fun main(args: Array<String>) {
println("Hello, World!")
val p1 = Person_1("jack", 21);
p1.lastName = "stephan"
p1.month = "may"
println("lastName is ${p1.getLastName}")
println("month is ${p1.getMonth}")
val p2 = Person_1("jack", 21);
p2.lastName = "knauth"
p2.month = "june"
println(p2.getLastName)
println(p2.getMonth)
class Person_1 (val name: String, val age : Int) {
//backing field 1
var lastName : String? = null
set(value) {
if (value?.length == 0) throw IllegalArgumentException("negative values are not allowed")
field = value
}
val getLastName
get() = {
lastName
}
//backing field 2
var month : String? = null
set(value) {
field = value
}
val getMonth
get() = {
month
}
}
output:
Hello, World!
lastName is () -> kotlin.String?
month is () -> kotlin.String?
() -> kotlin.String?
() -> kotlin.String?
You can just get rid of your getters like this:
If later you'll need them you can add it like this without api changes: