Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Kotlin for Java Developers Hello Kotlin! Finishing the Card Class

How kotlin differentiate between these variables????

In the below code how kotlin differentiate between the class variable and the constructor variables

class Student(val name: String, var age: Int){ val name: String = name var age: Int = age }

1 Answer

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Hey Varun!

Adding 'val' or 'var' to the variable in the constructor upgrades it to a member variable automatically; there's no need to redeclare it.

E.g. this is all you need.

class Student(val name: String, var age: Int)