Create New Instance of Kotlin Object

33,908

Solution 1

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.

Solution 2

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.

Solution 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)

Solution 4

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
Share:
33,908
AdamHurwitz
Author by

AdamHurwitz

Updated on November 04, 2020

Comments

  • AdamHurwitz
    AdamHurwitz over 3 years

    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))
    
  • AdamHurwitz
    AdamHurwitz over 6 years
    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.
  • AdamHurwitz
    AdamHurwitz over 6 years
    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.
  • Alex Yu
    Alex Yu over 6 years
    Of course! Because you wrote companion object - they are somewhat near/equivalent to static members in Java/C#.