You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use the primary constructor parameters to initialize additional class properties directly in the class body:
214
222
215
223
```kotlin
216
-
// Class with a primary constructor including default values for name and age
224
+
// Class with a primary constructor
225
+
// including default values for name and age
217
226
classPerson(
218
227
valname:String = "John",
219
228
varage:Int = 30
220
229
) {
221
-
// description property initialized in the class body using the primary constructor parameters
230
+
// description property initialized in the class body
231
+
// using the primary constructor parameters
222
232
val description:String="Name: $name, Age: $age"
223
233
}
224
234
@@ -232,7 +242,7 @@ fun main() {
232
242
```
233
243
{kotlin-runnable="true"}
234
244
235
-
Additionally, you can use a[trailing comma](coding-conventions.md#trailing-commas) in constructor declarations:
245
+
As with functions, you can use [trailing comma](coding-conventions.md#trailing-commas) in constructor declarations:
236
246
237
247
```kotlin
238
248
classPerson(
@@ -244,14 +254,16 @@ class Person(
244
254
245
255
### Initializer blocks
246
256
247
-
The primary constructor can't contain runnable code. Its purpose is to initialize the class and set its
248
-
properties. If you want to run some code during [instance creation](#creating-instances-of-classes), use _initializer blocks_ inside the class body.
257
+
The purpose of the primary constructor is to initialize the class and set its
258
+
properties. In most cases, this initialization doesn’t require complex code.
259
+
260
+
If you need to run complex code blocks when the primary constructor is executed during [instance creation](#creating-instances-of-classes),
261
+
use _initializer blocks_ inside the class body.
249
262
250
263
Declare initializer blocks with the `init` keyword followed by curly braces `{}`.
251
264
Write within the curly braces any code that you want to run during initialization:
252
265
253
266
```kotlin
254
-
//sampleStart
255
267
// Class with a primary constructor that initializes name and age
256
268
classPerson(valname:String, varage:Int) {
257
269
init {
@@ -265,7 +277,6 @@ fun main() {
265
277
// Creates an instance of the Person class
266
278
Person("John", 30)
267
279
}
268
-
//sampleEnd
269
280
```
270
281
{kotlin-runnable="true"}
271
282
@@ -306,6 +317,16 @@ fun main() {
306
317
You can use the primary constructor parameters in the initializer blocks. For example, in the code above, the first and second initializers use
307
318
the `name` and `age` parameters from the primary constructor.
308
319
320
+
A common use of `init` blocks is data validation. For example, by calling the [`require` function](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/require.html):
321
+
322
+
```kotlin
323
+
classPerson(valage:Int) {
324
+
init {
325
+
require(age >0, "age must be positive")
326
+
}
327
+
}
328
+
```
329
+
309
330
### Secondary constructors
310
331
311
332
In Kotlin, a secondary constructor is an additional constructor that a class can have beyond its primary constructor.
@@ -321,7 +342,9 @@ keyword inside the class body with the constructor parameters within parentheses
321
342
```kotlin
322
343
// Class header with a primary constructor that initializes name and age
323
344
classPerson(valname:String, varage:Int) {
324
-
// Secondary constructor that takes a String age and converts it to an integer
0 commit comments