Primary Constructor and Property Parameters
Declare classes with primary constructors and inline property declarations.
Primary Constructor and Property Parameters is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Primary Constructor
A Kotlin class declares its primary constructor right after the class name in parentheses. No method body — just a parameter list.
Simplest Class
The primary constructor without modifiers is invisible after the class name.
class Greeter(val name: String) {
fun greet() = "Hello, $name!"
}
fun main() {
println(Greeter("Ada").greet())
}Property Parameters
Prefix a constructor parameter with val or var to make it a class property in one step.
class Person(val name: String, var age: Int)
fun main() {
val p = Person("Bob", 30)
println("${p.name} is ${p.age}")
p.age = 31
println("Now ${p.age}")
}Parameters Without val/var
Constructor parameters without val/var are local to the constructor — they can be used in init blocks or property initializers, but not accessed later.
class Logger(prefix: String) {
val msg = "$prefix: ready"
}
fun main() {
val l = Logger("[INFO]")
println(l.msg)
// l.prefix // ERROR — not a property
}Default Parameter Values
Provide defaults for constructor parameters to avoid overload boilerplate.
class User(
val name: String,
val age: Int = 0,
val email: String = ""
)
fun main() {
println(User("Ana").age) // 0
println(User("Ben", 25).email) // ""
println(User("Cal", 30, "c@x.io").email)
}Named Arguments
Skip positional ordering and name the parameters at the call site — especially useful with defaults.
class HttpClient(
val baseUrl: String,
val timeoutMs: Int = 5000,
val retries: Int = 3
)
fun main() {
val c = HttpClient(
baseUrl = "https://api.example.com",
retries = 5
)
println("retries=${c.retries} timeout=${c.timeoutMs}")
}Visibility on Constructor
Use private constructor to prevent external instantiation — common when paired with factory methods.
class Singleton private constructor() {
companion object {
val instance = Singleton()
}
}
fun main() {
val s = Singleton.instance
println(s)
}Secondary Constructors
Add additional constructors with the constructor keyword. They must delegate to the primary constructor with this(...).
class Point(val x: Int, val y: Int) {
constructor(value: Int) : this(value, value)
}
fun main() {
println("${Point(3).x}, ${Point(3).y}") // 3, 3
}Constructor with Visibility
You can mix visibility per constructor — useful for libraries.
class Lib internal constructor(val key: String) {
companion object {
fun create(key: String) = Lib(key)
}
}
fun main() {
println(Lib.create("abc").key)
}init Block Recap
Code that should run during construction goes in an init { } block. Multiple init blocks run in source order.
class Validator(val value: String) {
init { require(value.isNotBlank()) { "value cannot be blank" } }
}
fun main() {
println(Validator("ok").value)
// Validator("") // throws IllegalArgumentException
}Combining Properties and Logic
Property declarations, secondary constructors, and init blocks compose into a flexible initialization pipeline.
class Account(
val owner: String,
initialBalance: Double = 0.0
) {
var balance: Double = initialBalance
private set
init {
require(initialBalance >= 0) { "balance must be non-negative" }
}
fun deposit(amount: Double) { balance += amount }
}
fun main() {
val a = Account("Alice", 100.0)
a.deposit(50.0)
println("${a.owner}: ${a.balance}") // Alice: 150.0
}Quick Check
What does the keyword val do when used in a primary constructor parameter?
Recap
The primary constructor lives on the class header. Use val/var for property parameters; bare parameters are constructor-local. Add defaults to reduce overloads, and run setup logic in init blocks. Secondary constructors delegate via this(...).
Frequently asked questions
Is the “Primary Constructor and Property Parameters” lesson free?
Yes — the full text of “Primary Constructor and Property Parameters” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Primary Constructor and Property Parameters”?
Declare classes with primary constructors and inline property declarations. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Primary Constructor and Property Parameters” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Primary Constructor and Property Parameters
- init Blocks and Initialization Order
- Custom Getters and Setters with field
- lateinit and Lazy Initialization