Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is 'ClassName.class' in Java same as '[ClassName::class]' in Kotlin?

On the Android dev page, in the Dagger section,

NetworkModule.class in Java code is written as [NetworkModule::class] in Kotlin code.

  1. I thought NetworkModule.class in Java should be NetworkModule::class.java in Kotlin, isn't it?

  2. 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 {
    ...
}
like image 268
starriet Avatar asked Dec 08 '20 01:12

starriet


People also ask

What is the difference between :: class and :: class Java in Kotlin?

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.

What does :: class mean in Kotlin?

:: 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.

What does this :: ClassName do in Java?

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.

What is the ClassName of a class Java?

It's just the name of the class.

How to get a class name in Java?

In this article, we looked at four methods to access a class name in Java. These methods are: getSimpleName (), getName (), getTypeName () and getCanonicalName ().

Why constructor name is same as class name in Java?

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.

Can the file name and class name be the same?

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.

Can a return type be the same as a class name?

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.


1 Answers

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.

like image 172
Sweeper Avatar answered Oct 05 '22 22:10

Sweeper