0

I am fairly new at kotlin. I know concepts of static member in java. According to documentation object works like static class / singleton but i cant seem to access them from my MainActivity. From examples, in kotlin i suppose do it as below but it seems not to be working for me. I am doing it wrong? I want to use object instead of companion object

TAG.kt

object TAG {
    var MainActivity : String? = null
}

MainActivity.kt

class MainActivity : AppCompatActivity(){  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Cannot find TAG.MainActivity as static variable like in Java
        TAG.MainActivity = MainActivity::class.java.canonicalName as String
    }
}

I have seen this posts but not working Static variables in kotlin?

3 Answers 3

5

Put the required object inside Companion Object whereas, it works like static in Kotlin

class MyClassWithStatics {
    companion object {
        const val SOME_CONSTANT = "Hello!"

        @JvmField var someUglyMutableField: String = "don't do this like ever"

        @JvmStatic fun hello() { println("$SOME_CONSTANT $someUglyMutableField") }
    }
}
// consuming Java code!
public class MyOtherClass {
    public void doSomething() {
        String constant = MyClassWithStatics.SOME_CONSTANT;
        String someUglyMutableField = MyClassWithStatics.someUglyMutableField;
        MyClassWithStatics.hello();
    }
}

Also, check this out to handle issue while accessing static object in Kotlin class https://kotlinlang.org/docs/tutorials/kotlin-for-py/objects-and-companion-objects.html

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, but i dont want to put it in a class, so i prefer to use object instead. But i cant seem to access it. I believe my implementation is same per documentation
3

Perhaps there is a TAG field/property somewhere else in your class? Is TAG imported properly?

This simple Kotlin program works with no errors:

object Obj {
  var Hello = "World"
}

fun main(args: Array<String>) {
  println(Obj.Hello)
}

Prints:

World

Comments

2

-----------UPDATED ANSWER----------

OP wants to have a top-level TAG object. The code OP provided in the question is actually correct.
The issue was that OP's Android Studio needed a restart.

----------MY ORIGINAL ANSWER (but still good for reference)----------

Putting it in a companion object is an option.

class MainActivity : AppCompatActivity() {  

    //You can name this object here too (like "companion object shared")
    companion object {
        var TAG: String? = null
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Notice the ordering here. In fact, you don't even have to type MainActivity. You can just type TAG = ...
        MainActivity.TAG = MainActivity::class.java.canonicalName as String
    }
}

6 Comments

Yes, but i dont want to put it in a class, so i prefer to use object instead. But i cant seem to access it. I believe my implementation is same per documentation
Oh I see. Ok, did you make sure the import is correct at the top of your file? Also did you make sure your TAG object is top level?
Yes, my import is correct. But Android studio doesnt seem to recognise it
I know it sounds dumb, but try restarting your computer and Android Studio because your code works for me.
WOW.. i just restarted my Android Studio and it works. Is it some kind of bug perhaps?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.