On the Android dev page, in the Dagger section,
NetworkModule.class
in Java code is written as [NetworkModule::class]
in Kotlin code.
I thought NetworkModule.class
in Java should be NetworkModule::class.java
in Kotlin, isn't it?
why use the square brackets([]
)? I think it's the same as get
, but why do we need it?
I was reading this: https://developer.android.com/training/dependency-injection/dagger-android#java
In the 'Dagger Modules' section,
Java code:
@Component(modules = NetworkModule.class)
public interface ApplicationComponent {
...
}
the same code written in Kotlin:
@Component(modules = [NetworkModule::class])
interface ApplicationComponent {
...
}
It is Kotlin Reflection API, that can handle Kotlin features like properties, data classes, etc. By using ::class. java , you get an instance of Class. It is Java Reflection API, that interops with any Java reflection code, but can't work with some Kotlin features.
:: is just a way to write a lambda expression basically we can use this to refer to a method i.e a member function or property for example class Person (val name: String, val age: Int) Now we can write this to access the person which has the maximium age.
The Classname. this syntax is used to refer to an outer class instance when you are using nested classes; see Using "this" with class name for more details.
It's just the name of the class.
In this article, we looked at four methods to access a class name in Java. These methods are: getSimpleName (), getName (), getTypeName () and getCanonicalName ().
Why the constructor name is same as the class name in Java? Every class object is created using the same new keyword, so it must have information about the class to which it must create an object. For this reason, the constructor name should be the same as the class name.
The myth about the file name and class name should be same only when the class is declared in public. Now, this .class file can be executed. By the above features, some more miracles can be done.
Finally, we can conclude that when we have a return type for the methods with the name same as a class name then that loses the features the constructors hold and that will behave like a method. And this is a bad practice in programming.
This is the array literal syntax supported only in annotations, added in Kotlin 1.2. You can use it to pass arrays of things to annotations. So [NetworkModule::class]
is actually an array containing a single element, that being NetworkModule::class
.
The Kotlin code translated to Java would be:
@Component(modules = { NetworkModule.class })
interface ApplicationComponent {
...
}
It's just that the {}
brackets can be omitted in Java, when there is a single element in the array. However, you can't just write NetworkModule::class
in Kotlin. You have to explicitly say that it's an array, using ways such as []
, or arrayOf
, unless it's the value
parameter, in which case it is translated as vararg
.
In general, NetworkModule.class
in Java should be translated to NetworkModule::class
when in an annotation. But note that this is of type KClass
. If you want a java.lang.Class
, add .java
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With