Couple of things are wrong here:
- Classes should always use camel-case for their names (
test
-> Test
)
- You cannot call another constructor as you tried to (calling
this(1)
inside of the other constructors body)
I think what you actually want is a
being a property and alternatively initialize it with a default value. You could do it like this
class Test(val a: Int) {
constructor() : this(1) // notice how you can omit an empty body
}
or even better, like this:
class Test(val a: Int = 1) // again an empty body can be omitted.
Edit:
If you need to do some calculations, as asked in the comment below Yole's answer:
class Test(val day: Int) {
// you can use any expression for initialization
constructor(millis: Long) : this(Date(millis).day)
}
or if things get more complicated:
class Test(var day: Int) {
// pass something (i.e. 1) to the primary constructor and set it properly in the body
constructor(millis: Long) : this(1) {
// some code
day = // initialize day
}
}