How to access Kotlin companion object in Java?

I convert one of my Java class to Kotlin and the class as below.

class MainApplication : Application() {
    companion object {
        operator fun get(context: Context): MainApplication {
            return context.applicationContext as MainApplication
        }
    }
}

It has a static function get.

I still have a Java function accessing it.

MainApplication application = MainApplication.get(mContext);

It was good when MainApplication is in Java. But not when MainApplication in Kotlin, the above code error

Error:(27, 54) error: cannot find symbol method get(Context)

How could I access get in my Java code above?

You can add @JvmStatic annotation to the method in companion object to make Kotlin generate a static method.

class MainApplication : Application() {
    companion object {
        @JvmStatic fun get(context: Context): MainApplication {
            return context.applicationContext as MainApplication
        }
    }
}

you can then access it from Java like before converting to Kotlin:

MainApplication application = MainApplication.get(mContext);

EDIT: I feel obliged to add something I learned recently: @JvmStatic doesn't actually move where the method gets generated. It duplicates it, by generating a static method for Java in addition to the method on the companion object. Personally I think this isn't great and it can have some implications depending on a use case, so something worth knowing.

Calling Kotlin from Java, The companion object is a singleton, and its members can be accessed directly via If you're integrating with Java code and need a true static member, you can � Here, we created an object p1 of the Person class to call callMe() method. That's how things normally work. However, in Kotlin, you can also call callMe() method by using the class name, i.e, Person in this case. For that, you need to create a companion object by marking object declaration with companion keyword.

Ops, I got it. Just use the below.

MainApplication application = MainApplication.Companion.get(mContext);

Objects and companion objects, Unlike Java or C#, Kotlin doesn't have static members or member functions. be called without having a class instance but needs access to the internals of a class , By declaring a companion object inside our class, you'll be able to call its� In Kotlin or any other programming language like Java and C#, whenever we want to call the method or whenever we want to access the members of a class then we make the object of the class and with the help of that object, we access the members of the class.

By omitting the name of your companion object, the name Companion must be used to access the methods.

Example:

class MyClass1 {
    companion object Object1 {
        fun method1 {
        }
    }
}

class MyClass2 {
    companion object {
        fun method2 {
        }
    }
}

To invoke the first companion object method you would do the following:

MyClass1.method1()

To invoke the second:

MyClass2.Companion.method2()

See the Kotlin docs on Companion Objects for details.

Companion object in Kotlin � Suneet Agrawal, In Kotlin or any other programming language like Java and C#, whenever we want to call the method or whenever we want to access the� Unlike Java or C#, Kotlin doesn’t have static members or member functions. Kotlin recommends to simply use package-level functions instead. If you need to write a function that can be called without having a class instance but needs access to the internals of a class, you can write it as a member of a companion object declaration inside that class.

You may encounter a problem where you cannot access the Companion object's method in Java if the new keyword is used in the method call. The new keyword should be omitted. The documentation states:

Companion objects and their members can only be accessed via the containing class name, not via instances of the containing class.

So if you have a class like this:

class MyClass {
    companion object {
        fun create() {}
    }
}

You can call the companion object's method like this:

MyClass.create()

But not like this:

new MyClass.create

Companion object in Kotlin, You can simply access the companion objects as static fields, and properties using getters and setters : AppHelper helper = App.instance. If we don’t want to create one class instance and access its members, we need to create one companion object. Unlike other programming languages like Java or Python, Kotlin doesn’t have the concept of static function. How to declare a companion object : Companion objects are declared using companion keyword.

Calling Kotlin from Java: start using Kotlin today, A true companion: exploring Kotlin's companion objects When you need to access this functionality from Java you can access it in 2 possible� Kotlin Companion Object. Kotlin doesn’t have static keyword. So how do we set static variables and methods? companion object is the answer. It is equivalent to static objects in Java. A companion object is common to all instances of the classes. It can access all members of the class too, including the private constructors.

A true companion: exploring Kotlin's companion objects, Properties in Kotlin that are declared within a named object or a companion object, are used as static fields in Java. To access these fields in Java, these must� Object Expressions and Declarations. Sometimes we need to create an object of a slight modification of some class, without explicitly declaring a new subclass for it. Kotlin handles this case with object expressions and object declarations. Object expressions. To create an object of an anonymous class that inherits from some type (or types), we

Java Interoperability - Calling Kotlin from Java, Learn what objects are in Kotlin and how they can make your code cleaner If you have some solid experience in the Java ecosystem (6+ years), and allow their members to be accessed from inside the companion class� Kotlin: Basics of Companion Objects. {companion object Beverage And we can access the drink method just like we can access other properties in the companion object:

Comments
  • Nice one. Looks like there are two ways. Depends on which side (kotlin or java) we could change.
  • Tick this as 'the' answer, given it's more likely one had to make the Kotlin compatible with Java than vice versa. (i.e. people likely to convert from Java to Kotlin, and work on the Kotlin code instead of Java code).
  • The other answer (mine) i.e. using MainApplication.Companion.get(mContext); is also a good answer :)
  • according to official documentation: kotlinlang.org/docs/reference/java-to-kotlin-interop.html you should use @JvmField in this scenario and NOT @JvmStatic
  • You can also name the companion object if you don't like the default Companion name.
  • As of Kotlin 1.1.50 you can call functions of the companion object by omitting the companion key word
  • not if you are calling the companion object from Java and it doesn't have a name.
  • I had to use MyClass1.Object1.method1(), but this worked best, thanks!