what's the difference of defining: class MyClass(v...
# announcements
c
what's the difference of defining: class MyClass(val a:String, val b:String) from class MyClass(a:String, b:String)
b
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
Copy code
class Foo (a: String) {
    val a: String = a
}
c
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
they can also use
var
but they should all be properties certainly