0

I have two different pieces of code. In one i need to use object and in the second i'm not. Can someone explain me the difference between the situation: first Code:

   private val onInitWebResponseHandler: VolleyHandler.WebResponseHandler = VolleyHandler.WebResponseHandler()
{
    Thread(ParseJsonStringOnInit(WeakReference(this),
            weakRefIOnAllScoresDataFirstFetched, it)).start()

}

Second Code:

val competitionOrderLevelComparator : Comparator<CompetitionObj> = object : Comparator<CompetitionObj> {
    override fun compare(object1: CompetitionObj, object2: CompetitionObj): Int
    {
        return object1.orderLevel - object2.orderLevel
    }

}


      fun interface  WebResponseHandler
{
    fun onWebResponseFinished(jsonString:String?)
}

In addition how the first code, we can have () brackets if interface doesn't have a constructor?

Eitanos30
  • 1,331
  • 11
  • 19
  • This should explain it: [how-to-create-an-instance-of-anonymous-interface-in-kotlin](https://stackoverflow.com/questions/37672023/how-to-create-an-instance-of-anonymous-interface-in-kotlin) and [cant-understand-how-to-use-sam-interface-in-kotlin](https://stackoverflow.com/questions/64793715/cant-understand-how-to-use-sam-interface-in-kotlin) – ChristianB Jan 09 '21 at 11:37
  • @ChristianB, thanks!!! but can you explain how the **()** works in the first example:`VolleyHandler.WebResponseHandler()` since it's an interface – Eitanos30 Jan 09 '21 at 12:13
  • Can you show the definition of `VolleyHandler.WebResponseHandler` ? – ChristianB Jan 09 '21 at 12:35
  • @Animesh Sahu, correct me if I am wrong, then `VolleyHandler.WebResponseHandler` can't be an interface, as @Eitanos30 is able calling a constructor on it. A SAM would called (without constructor) like `SamInterface { ... }` – ChristianB Jan 09 '21 at 12:54
  • @ChristianB, i have added the `VolleyHandler.WebResponseHandler` – Eitanos30 Jan 09 '21 at 12:55
  • If i'm putting the first `{` immediately after `VolleyHandler.WebResponseHandler` (at the same line of code) i don't need the `()`, **but** if i put the open `{` one line below `VolleyHandler.WebResponseHandler`, then i must add the `()` – Eitanos30 Jan 09 '21 at 12:57
  • 2
    @ChristianB SAM-converted lambdas have an optional empty constructor you can call, which is necessary if you open the brackets on the next line. – Tenfour04 Jan 09 '21 at 18:31

0 Answers0