Making private constructor and creating secondary constructor gives error in kotlin

Issue

Below code snippet gives error. Can someone guide why this is happening?

class Test() private constructor {
        
    constructor(name: String): this() {
        println("test called constructor $name")
    }
   
}

fun main() {
    Test("hk")
}

Removing private constructor , this is working.

I tried to resolve this on my side. but I got no success.

Solution

class Test private constructor() {

  constructor(name: String): this() {
    println("test called constructor $name")
  }

}

fun main() {
  Test("hk")
}

Answered By – Sunny

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *