https://kotlinlang.org logo
Title
c

coder82

11/15/2018, 5:34 PM
what's the difference of defining: class MyClass(val a:String, val b:String) from class MyClass(a:String, b:String)
b

Burkhard

11/15/2018, 5:36 PM
If you use
class Foo(a: String, b: String)
you say that you want the primary constructor to take 2 arguments (a and b). If you add
val
or
var
you also create 2 properties in the class which will automatically get assigned the arguments passed to the constructor.
So
class Foo(val a: String)
is a short for
class Foo (a: String) {
    val a: String = a
}
c

coder82

11/15/2018, 5:40 PM
great that's what I thought as well trying to test it eheheh
and that's why data classes can only be with val, makes sense
p

phldavies

11/15/2018, 5:50 PM
they can also use
var
but they should all be properties certainly