Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create New Instance of Kotlin Object

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))
like image 276
Adam Hurwitz Avatar asked Nov 01 '17 00:11

Adam Hurwitz


People also ask

How do I create a new instance of a class in Kotlin?

Similar to using the fun keyword in Kotlin to create a new function, use the class keyword to create a new class. You can choose any name for a class , but it is helpful if the name indicates what the class represents. By convention, the class name is written in Upper Camel Case (also called Pascal Casing).

How do I create a new instance of an object?

Answer. To create a new instance of an object, we use the "new" keyword. This keyword creates a new instance of an object, which we can then assign to a variable, or invoke methods. For example, to create a new StringBuffer object, we would use the new keyword in the following way.

How do you create a object on Kotlin?

We can create an object using the reference of the class. Accessing the property of the class: We can access the properties of a class using an object. First, create an object using the class reference and then access the property.


4 Answers

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
like image 58
nPn Avatar answered Oct 21 '22 20:10

nPn


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.

like image 28
Adam Hurwitz Avatar answered Oct 21 '22 20:10

Adam Hurwitz


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)

like image 34
Alex Yu Avatar answered Oct 21 '22 21:10

Alex Yu


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.

like image 9
Kiskae Avatar answered Oct 21 '22 21:10

Kiskae