Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about Kotlin's companion object definition

Tags:

kotlin

When i reach the companion object section in the ebook "Kotlin in action" it said that:

"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 an object declaration inside that class"

As my understanding this means a function of the companion object can access the method and properties of the class that contain it. But when i try to implement this i can't access the members of the class from its companion object'function:

class Normal() {

var name: String = "hallo"


companion object {
    fun printName() {
        println(name) // ERROR!!! unresolved reference name
    }
}}

Did i misunderstood about this concept?

like image 905
shinobitiger310 Avatar asked May 16 '18 09:05

shinobitiger310


People also ask

What are Kotlin's companion objects?

companion object is how you define static variables/methods in Kotlin. You are not supposed to create a new instance of Retrofit / ApiService each time you execute a request, however.

What is a companion object?

A companion object is an object that's declared in the same file as a class , and has the same name as the class. A companion object and its class can access each other's private members. A companion object's apply method lets you create new instances of a class without using the new keyword.

What is the difference between object and companion object?

Object declarations are very useful for implementing the Singleton pattern. And the getInstance method can then be invoked like this. A companion object is a specific type of object declaration that allows an object to act similar to static objects in other languages (such as Java).

What is the benefit of companion object?

Advantages of Companion Objects in Scala Companion objects provide a clear separation between static and non-static methods in a class because everything that is located inside a companion object is not a part of the class's runtime objects but is available from a static context and vice versa.


1 Answers

Method inside companion are kind of static by default(compared to Java & also this is how you achieve static kind of things in Kotlin) and you can not access normal variable from static method.

Same is happening here.

Edit:-

The definition in book is confusing, A companion object is not part of an instance of a class. You can't access members from a companion object, just like in Java you can't access members from a static method. But in case of utility classes where you just need to perform some operation you can call Static method which create a new instance of class and the perform some functions.

For example you can check answer by @user8320224, I am also quoting his code here,

class Normal {
private var name: String = "hallo"
private fun printName() {
    println(name)
}

companion object {
    fun factoryNormal(): Normal {
        val normal = Normal()
        normal.printName()
        normal.name = "new name"
        normal.printName()
        return normal
    }
}
}
like image 200
Randheer Avatar answered Dec 16 '22 04:12

Randheer