https://kotlinlang.org logo
#android
Title
# android
a

Aurelian Ticușan

05/18/2020, 3:47 PM
Hello I have a question relating inner classes in Java I'm having troubles with. I have a utility class in which I hold various enum classes. There is no problem accessing them in Java classes, but when I try to access them in a Kotlin class, I get Unresolved reference: UtilityClass Utility class looks like this
Copy code
public final class UtilityClass {

    private UtilityClass() {
        // Utility class
    }

    public enum FirstEnum {
        FIRST_OPTION, SECOND_OPTION, THIRD_OPTION, NONE
    }
}
While the the data class in Kotlin is as follows
Copy code
data class KotlinClass(val option: UtilityClass.FirstEnum = UtilityClass.FirstEnum.NONE)
Does anyone maybe know why I get "Unresolved reference: UtilityClass" trying to access the enum from my utility class? Both classes are in same package, so no imports required, I've tried moving either class in another package, but the issue persists I had no luck finding a solution for this, I can't find anything even remotely similar to this problem
d

Dustin Lam

05/18/2020, 3:48 PM
Your utility class isn't static
a

Aurelian Ticușan

05/18/2020, 3:50 PM
Yes, but I cannot make a top level class static
d

Dustin Lam

05/18/2020, 3:58 PM
Oh, I thought it was related to inner class, but actually I just tried this out and it works fine O.o
a

Aurelian Ticușan

05/18/2020, 4:11 PM
For me it doesn't work. I have kotlin version 1.3.61, although I don't think it's relevant
d

Dustin Lam

05/18/2020, 4:38 PM
I tried on 1.3.72
a

Aurelian Ticușan

05/18/2020, 5:39 PM
I tried this on a different project and it worked. Maybe it's a problem with the project, but I have no idea what exactly.
c

Cody Engel

05/18/2020, 5:49 PM
If you are trying to write a utility class in Kotlin you'd probably have better luck defining it as an
object
instead of a
class
a

Aurelian Ticușan

05/18/2020, 5:57 PM
Yes, but the Utility class should remain Java class. That's the problem, having an kotlin object as the utility class works, I get the error when the utility class is Java class
c

Cody Engel

05/18/2020, 6:05 PM
What's the stack trace? The unresolved reference makes it sound like the import is missing. I recreated this on my end and it was working for me
a

Aurelian Ticușan

05/18/2020, 6:10 PM
There shouldn't be import problems, as both classes are from same package. This is the stacktrace
Untitled
g

gildor

05/19/2020, 1:48 AM
It’s not an actuall compilation error, it’s gradle stacktrace, check your build logs
a

Aurelian Ticușan

05/19/2020, 9:10 AM
But the build logs says the error I don't understand ,*Unresolved reference: UtilityClass"
g

gildor

05/19/2020, 9:40 AM
Okay, make sense
so it cannot resolve this class
could you show screenshot from IDE? is it resolved?
Maybe some another UtilityClass is imported, so it doesn’t see your default package import
Because code which you show it fine and it should work. Maybe you could reporoduce on a sample project
also, you sure that your Kotlin class has package itself?
2 Views