Mehroze Yaqoob
Mehroze Yaqoob

Reputation: 1041

Why do class compile when package name is wrong in Kotlin?

I just saw my class compile with wrong package name when I was writing code in kotlin. Any idea how and why that happened.

Thanks

Upvotes: 1

Views: 1153

Answers (2)

Arbaz Pirwani
Arbaz Pirwani

Reputation: 965

Hello there in kotlin package not works like they works in java

In kotlin you can set any package with any name any where

For example this is your package hierarchy

com->android->kotlin->ui->activities

In where you have file MainActivity.kt and Utils.kt

In your MainAcitivity file you can set this package name as

package com.android.kotlin.ui.activities

And on the same time you can set your Utils file package to

package com.android.kotlin.utilities 

So why this happen and Kotlin complier not generate an error and how it works fine?

In kotlin you can say folder package are not really exists you can save your file to any folder like your abc file in xyz folder and your efg file into vwx folder and you can set both of your file package to com.android.lmnop package and you can access properties, top level function classes etc directly without any problem and without compile time error! hope you understand.

And you can even get knowledge about kotin packaged from below mentions resource.

https://kotlinlang.org/docs/reference/packages.html

https://code.tutsplus.com/tutorials/kotlin-from-scratch-packages-basic-functions--cms-29445

Upvotes: 1

gpunto
gpunto

Reputation: 2822

The package declaration tells the compiler which package the file belongs to. That will be the file's package, even if the file location doesn't match.
More informations here.

Upvotes: 0

Related Questions