Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access outer class' javaClass.simpleName from companion object in kotlin?

I would like to be able to access the simpleName of my class from it's companion object.

I would like this:

val o1 = Outer("foo")
val o2 = Outer("bar")

to print the following output:

Outer: hello
Outer: foo
Outer: bar

The actual use case would be this in java:

class Outer {
    static final String TAG = Outer.class.simpleName();
    // and now I'm able to use Outer.TAG or just TAG in both static and non-static methods
}

I tried 2 things:

  1. assign Outer's simpleName to the companion object's COMPANION_TAG and then use COMPANION_TAG from companion's init and all over Outer's functions. I can access COMPANION_TAG from everywhere I need, but unfortunately I can only get "Companion" and not "Outer" this way.

  2. access Outer.OUTER_TAG from the companion object's init. Here the problem is that I can't find the way to access it.

Here's the code:

class Outer(str: String) {
    private val OUTER_TAG = javaClass.simpleName
    companion object {
        @JvmStatic val COMPANION_TAG = PullDownAnimationLayout.javaClass.simpleName // gives "Companion" :(
        init {
            // how can I access OUTER_TAG?
            Log.d(OUTER_TAG, "hello") // this gives an error
        }
    }
    init {
        Log.d(OUTER_TAG, str) // Outer: ... :)
        Log.d(INNER_TAG, str) // Companion: ... :(
    }
}

val o1 = Outer()
val o2 = Outer()
like image 447
Gavriel Avatar asked Jan 31 '18 02:01

Gavriel


People also ask

How do you call a method from a companion object?

In some languages like Java and C#, we use static keyword to declare the members of the class and use them without making any object i.e. just call them with the help of class name. So, to call a method named myMethod() having a class name as MyClass , we will use: MyClass. myMethod();

Can we access the members of the class by class name only using companion object?

Answer: Since companion objects are equivalent to the Static class, any variables or methods can be directly accessed from the containing class. Let's understand this with the help of a simple code example.

What is the difference between companion object and object in Kotlin?

An object, or an object declaration, is initialized lazily, when accessed for the first time. A companion object is initialized when the corresponding class is loaded. It brings about the 'static' essence, although Kotlin does not inherently support static members. Show activity on this post.

What does companion object do in Kotlin?

The companion objects can access private members of the class. Hence, they can be used to implement the factory method patterns.


1 Answers

In order to achieve this in Kotlin,

class Outer {
    static final String TAG = Outer.class.simpleName();
    // and now I'm able to use Outer.TAG or just TAG in both static and non-static methods
}

It should be

class Outer {
    companion object {
        val Tag = Outer::class.java.simpleName
        val Tag2 = Outer.javaClass.simpleName // This will not work
    }
}

println(Outer.Tag)  // print Outer
println(Outer.Tag2) // print Companion

I think you misunderstand what companion is. companion is similar to Java static. See this discussion.

like image 78
Joshua Avatar answered Oct 04 '22 02:10

Joshua