How the lazy declaration is working here?

Viewed 143

I am following a course named "Getting Data from the Internet" from the official android developers' website. It shows how to get data from a URL using Retrofit and Moshi library. They throw this code and I am not able to understand how the code inside object MarsApi{..} is working. Can anyone here explain this to me?

private const val BASE_URL = "https://android-kotlin-fun-mars-server.appspot.com"
private val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .build()
private val retrofit = Retrofit.Builder()
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl(BASE_URL)
    .build()

interface MarsApiService{
    @GET("photos")
   suspend fun getPhotos():List<MarsPhoto>

}

object MarsApi {
val retrofitService : MarsApiService by lazy {
    retrofit.create(MarsApiService::class.java)
}

//Why I can't write it as: 
//    val retrofitService2 : MarsApiService = object : MarsApiService{
//        override suspend fun getPhotos(): List<MarsPhoto> {
//            return retrofit.create(MarsApiService::class.java)
//        }
//    }
}
1 Answers
Related