Create New Instance of Kotlin Object
Asked Answered
F

4

10

I have an object QuickSort that I am attempting to create 2 instances of. When I try to create 2 separate instances I can see that it is only using one instance because I have a count in the QuickSort class that is inaccurate. Kotlin does not use new in the syntax, so how would I go about this?

object QuickSort {
      var count = 0;
      quickSortOne(...){
          ...
          count++
          ...
      }
      quickSortTwo(...){
          ...
          count++
          ...
      }
  } 

Here is how I am attempting to create my 2 instances.My goal is to have quickSort1 and quickSort2 be 2 separate instances.

var quickSort1 = QuickSort
quickSort1.quickSortOne(...)

var quickSort2 = QuickSort
quickSort2.quickSortTwo(...)

Attempted Solution: Converting QuickSort from an object to a class. This still results in the same instance being used as seen by the count of the second method including the first calls count.

class QuickSort {
      var count = 0;
      quickSortOne(...){
          ...
          count++
          ...
      }
      quickSortTwo(...){
          ...
          count++
          ...
      }
  }

...

var quickSortFirst = QuickSort()
printTest(quickSortFirst.quickSortFirst(arrayList, 0, arrayList.size - 1))

var quickSortLast = QuickSort()
printTest(quickSortLast.quickSortLast(arrayList, 0, arrayList.size - 1))
Fatherhood answered 1/11, 2017 at 0:9 Comment(5)
Adam, I suppose you're joking! Why do you create companion object ? It's purpose is: Note that, even though the members of companion objects look like static members in other languages, at runtime those are still instance members of real objects,...Byrle
Just remove companion object and make your methods as normal members of class.Byrle
Thanks @Ingaz! I removed the companion object{} and updated my code exactly as implemented above. It still is using the same QuickSort() instance.Fatherhood
Congratulations! Kotlin is great language, practical and not ugly in the same time. Also I still can't understand what you tried to accomplish. Kotlin has online playground: try.kotlinlang.org/#/UserProjects/9ck28rcs1gco73cerr7ql2hi2g/…Byrle
My goal is to have quickSort1 and quickSort2 be 2 separate instances. Your attempted solution does just that. Full code would be helpful, there is a lot of suspicious looking bits. (Where did quickSortFirst method suddenly come from? Is quickSortOne a function? Both of them operate on the same array list, is that ok?)Unionist
F
2

I figured out my issue. I was passing in the same ArrayList to both quickSortOne() and quickSortTwo(). Since the ArrayList was being modified by the first method, the second method was being affected as well.

Fatherhood answered 4/11, 2017 at 5:42 Comment(0)
T
9

object is a kotlin's version of a singleton. By using it you specify that there will only be 1 instance that is shared by all its users.

Turn it into a class and you will be able to create individual instances.

Tarantella answered 1/11, 2017 at 0:18 Comment(1)
Converting QuickSort from an object to a class still results in the same instance being used as seen by the count of the second method including the first calls count.Fatherhood
B
3

Adam, you miss the whole point: objects are singeletones by design.

It's one of good scala featuras copied into kotlin.

(In java or C# you have ugly static members mixed with normal class members).

As for your question:

A. No you can't make two instanse of kotlin/scala object as you can't have two different String in Java/C#.

B. Just replace object with class (and do you really need new? I never missed it in python)

Byrle answered 1/11, 2017 at 0:23 Comment(2)
Converting QuickSort from an object to a class still results in the same instance being used as seen by the count of the second method including the first calls count.Fatherhood
Of course! Because you wrote companion object - they are somewhat near/equivalent to static members in Java/C#.Byrle
A
3

You definitely want to use a class as the object keyword creates only the one instance. You can use a companion object within the class to keep track of the number of instances of the class that get created.

Here is an example which you also try here

data class QuickSort(val objectName: String) {
    init {
        count++ 
    }
    companion object {
        var count = 0
        fun printCount() = println("count = $count")
    }   
}

fun main(args: Array<String>) {
    QuickSort.printCount()
    val quickSort1 = QuickSort("qs1")
    QuickSort.printCount()
    val quickSort2 = QuickSort("qs2")
    QuickSort.printCount()  
    // just to prove you have two instances.
    println(quickSort1)
    println(quickSort2)
    println("quickSort1 hashcode = ${quickSort1.hashCode()}, quickSort2 hashcode = ${quickSort2.hashCode()}")
}

which produces :

count = 0
count = 1
count = 2
QuickSort(objectName=qs1)
QuickSort(objectName=qs2)
quickSort1 hashcode = 112207, quickSort2 hashcode = 112208
Avoidance answered 1/11, 2017 at 19:39 Comment(0)
F
2

I figured out my issue. I was passing in the same ArrayList to both quickSortOne() and quickSortTwo(). Since the ArrayList was being modified by the first method, the second method was being affected as well.

Fatherhood answered 4/11, 2017 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.