简体   繁体   中英

What is the meaning of ::class.java

I am trying to access the web api through Retrofit2 so I have created a builder for that in which I don't understand what is this line doing how I am able to pass my interface in that

 val retrofitBuilder = Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build()

                //meaning of this line
                **.create(ApiInterface::class.java)**

Here is the Interface

interface ApiInterface {

    @GET("users")
    fun getData():Call<List<FakeJsonDataItemItem>>

}

what am I passing in there?

By using ::class.java , you get an instance of Class. It is Java Reflection API.

If you want to get Java class reference, you have to use.java property on a KClass instance:

val c = ExampleClass::class.java  // reference type of java

And by using ::class , you get an instance of KClass. It is Kotlin Reflection API. When we create an object using any class type as below the reference type will be type of KClass.

val c = ExampleClass::class  // reference type of KClass

Retrofit create() method behind the scene validates the interface. After that, if the interface is validated an API interface instance is created using Retrofit, the instance returned from retrofit is actually a proxy class, a dynamic implementation of the interface at runtime.

Class objects basically describe a particular type, like a Class<String> is a special object that describes the String class.

They contain useful information like what fields that type has, what methods it has etc. That might be what your Retrofit call is doing - taking an interface definition, looking at what methods are defined on it (and important annotations and whatever else), and generating some code that matches it.

A really common use of Class objects is as a type definition that you can pass around and refer to. You see it a lot in generic functions.

They also let you create instances of that type, by calling getConstructor() and then newInstance() - this can be useful if you want to say "here's the type of object I want you to create" without hardcoding specific constructor calls for each type. You could (for example) create a function that creates and displays Fragment s in Android, which you call by passing in the Class of a fragment type, and let the function invoke the default constructor and create an instance whenever it's appropriate.


In Kotlin, instead of MyClass.class you call MyClass::class - but that gives you a KClass object, which is a Kotlin version of Class which some Kotlin-specific extras (things like whether it's a data class , etc).

If you want the equivalent Java Class object (which most libraries will be using unless they're Kotlin-specific) you call the java property on the KClass , ie MyClass::class.java , which gives you the Class<MyClass> object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM