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 Build a Simple Android App with Kotlin Improving Our Code Creating a New Class

Vladimir Vasconcelos
PLUS
Vladimir Vasconcelos
Courses Plus Student 6,433 Points

What am I doing wrong?

I think my code is ok, but I'm still getting an error.

PictureBook.kt
public fun PictureBook(){

  var title : String = "Android Adventures"
}
Raveesh Agarwal
Raveesh Agarwal
2,994 Points

Hi! I tried this question just now. This passes:

public class PictureBook{
    val title = "Android Adventures"
}

You would want to use val instead of var whenever possible. This will lead to more robust code. Use var only when you absolutely need to change the variable.

3 Answers

Seth Kroger
Seth Kroger
56,413 Points

You should be using class instead of fun.

Todd Anderson
Todd Anderson
4,260 Points

Try var title = "Android Adventures";

:)

Vladimir Vasconcelos
Vladimir Vasconcelos
Courses Plus Student 6,433 Points
public fun PictureBook(){

var title = "Android Adventures"
}

I got:

Bummer! Make sure PictureBook has a 'title' property and that it's a String
Todd Anderson
Todd Anderson
4,260 Points

Hmm... Mine passes. I do notice you forgot the semi-colon after "Android Adventures";

public class PictureBook(){
  var title = "Android Adventures";
}
Raveesh Agarwal
Raveesh Agarwal
2,994 Points

no. in Kotlin, "; " is not required always use val where possible. Declare var only when you absolutely need to