Just started using Kotlin and created some activities as Kotlin Files. They work, but still, I want to know what the difference between creating a File and a Class.
Kotlin Class: Android Studio displays it with no extension if the file contains ONLY ONE class.
Unlike Java, Kotlin allows you to put certain things outside the class like:
- extension functions
- constants
If you, therefore, add any of the above or another class to the file, Android Studio will change it to "Kotlin File":
Removing the above extras, will again show the file as "Kotlin Class"
The only difference is that creating a file creates a file with no classes, and creating a class creates a file with one class. You can then add more classes to the file, or delete classes, or make any other changes - the end result doesn't depend on how the file was created.
Kotlin Class: Android Studio displays it with no extension if the file contains ONLY ONE class.
Unlike Java, Kotlin allows you to put certain things outside the class like:
- extension functions
- constants
If you, therefore, add any of the above or another class to the file, Android Studio will change it to "Kotlin File":
Removing the above extras, will again show the file as "Kotlin Class"
private const val TAG = "MyActivity"
at the top simply for the purpose of Log.d messages, it turns my class into a file. I know it's not really a problem, but is there a better place to put that? –
Firehouse Here is the answer from the official Documentation:
If a Kotlin file contains a single class (potentially with related top-level declarations), its name should be the same as the name of the class, with the .kt extension appended. If a file contains multiple classes, or only top-level declarations, choose a name describing what the file contains, and name the file accordingly. Use camel humps with an uppercase first letter (e.g. ProcessDeclarations.kt).
The name of the file should describe what the code in the file does. Therefore, you should avoid using meaningless words such as "Util" in file names.
so basically a file can -for instance- contains just (helper-)functions without any class declaration.
As I can see Kotlin file has many classes inside it But a Kotlin class has one class with that name, that's only an observation though.
Yes, if you define more than one class, it will be automatically converted to file, otherwise it will be automatically converted to class. Usually I think if you need to define some variables or methods that can be directly referenced, you can put them in the file.
Creating a file creates public final class at bytecode level just like creating a class. If You declare a class in a file then two classes will be created at the bytecode level. Therefore, the file exists for convenience - it contains code unrelated to defining the behavior of objects
The difference is:
Kotlin Class can have only a class.
Kotlin File can have many classes.
I found a further difference - when it comes to renaming files, the refactoring mechanism handles Kotlin files and class files differently:
- Class: On renaming the file, the refactoring also renames the contained class itself. This also works vice-versa, so when the class is renamed, also the file is renamed.
- File: When renaming a file, only the file is renamed and the contained class name is untouched.
I assume this is done that way because the class name should be kept consistent to the file name in case of Kotlin class files, for a better overview.
© 2022 - 2024 — McMap. All rights reserved.
private const val TAG = "MyActivity"
at the top simply for the purpose of Log.d messages, it turns my class into a file. I know it's not really a problem, but is there a better place to put that? – Firehouse