
Here's the rewrite — same content, same code, but written like an actual developer who made a YouTube video and is sharing it casually:
Hey everyone! Just dropped a full video on Kotlin Constructors — here's a quick summary if you're short on time.
1. Primary Constructor
In Kotlin, the constructor goes right in the class header. No separate block, no boilerplate.
kotlin
class User(var name: String, var age: Int)
fun main() {
val alice = User("Sid", 21)
println(alice.name) // Sid
println(alice.age) // 21
}
That's it. Declare and move on.
2. Init Block
Need to run some logic the moment an object is created? Use init. It fires automatically — you don't call it yourself.
kotlin
class User(var num1: Int, var num2: Int) {
val result = num1 * num2
init {
println(result) // runs on creation
}
}
fun main() {
val num = User(34, 67) // prints 2278
}
3. Secondary Constructors
Sometimes you need more than one way to build an object. Secondary constructors handle that — but they have to delegate to the primary using this(...).
kotlin
class Man(var name: String, var age: Int) {
constructor(name: String) : this(name, 18) // default age = 18
constructor() : this("Sid", 12) // everything defaulted
}
fun main() {
val user1 = Man("Sasuke", 2)
val user2 = Man()
println(user1.name) // Sasuke
println(user2.name) // Sid
}
4. Companion Object
Companion objects belong to the class, not to any instance — Kotlin's answer to static methods.
kotlin
class Calculator(val num1: Int, val num2: Int) {
init {
println("Sum: ${num1 + num2}")
println("Product: ${num1 * num2}")
println("Division: ${num1 / num2}")
}
companion object {
fun add(a: Int, b: Int) = a + b
fun multiply(a: Int, b: Int) = a * b
fun divide(a: Int, b: Int) = a / b
}
}
// No object needed:
Calculator.add(45, 9) // 54
Full video with live coding and a challenge is on my channel. Drop questions in the comments if anything's unclear!