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?

7
  • This should explain it: how-to-create-an-instance-of-anonymous-interface-in-kotlin and cant-understand-how-to-use-sam-interface-in-kotlin Commented Jan 9, 2021 at 11:37
  • @ChristianB, thanks!!! but can you explain how the () works in the first example:VolleyHandler.WebResponseHandler() since it's an interface Commented Jan 9, 2021 at 12:13
  • Can you show the definition of VolleyHandler.WebResponseHandler ? Commented Jan 9, 2021 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 { ... } Commented Jan 9, 2021 at 12:54
  • 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. Commented Jan 9, 2021 at 18:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.