Skip to content

Commit e3e1830

Browse files
SME review
1 parent 33b0bad commit e3e1830

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

docs/topics/classes.md

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,22 @@ Both the class header and class body can be minimal.
3333
If the class does not have a body, you can omit the curly braces `{}`:
3434

3535
```kotlin
36-
// Class with name and age properties declared in the primary constructor, but without a body
36+
// Class with primary constructor, but without a body
3737
class Person(val name: String, var age: Int)
3838
```
3939

4040
Here's an example of a class with a header, body, and an [instance created](#creating-instances-of-classes) from it:
4141

4242
```kotlin
43-
// Person class with a primary constructor that initializes the name property
43+
// Person class with a primary constructor
44+
// that nitializes the name property
4445
class Person(val name: String) {
4546
// Class body with age property
4647
var age: Int = 0
4748
}
4849

4950
fun main() {
50-
// Creates an instance of the Person class by calling the constructor
51+
// Creates an instance of Person class by calling the constructor
5152
val person = Person("Alice")
5253

5354
// Accesses the instance's properties
@@ -62,9 +63,9 @@ fun main() {
6263
## Creating instances of classes
6364

6465
An instance is created
65-
when you use the class as a blueprint to build a real thing to work with in your program.
66+
when you use the class as a blueprint to build a real object to work with in your program.
6667

67-
To create an instance of a class, use the class name followed by parentheses (`()`), similar to calling a function:
68+
To create an instance of a class, use the class name followed by parentheses (`()`), similar to calling a [function](functions.md):
6869

6970
```kotlin
7071
// Creates an instance of the Person class
@@ -79,10 +80,12 @@ In Kotlin, you can create instances:
7980
You can assign the created instance to a mutable (`var`) or read-only (`val`) [variable](basic-syntax.md#variables):
8081

8182
```kotlin
82-
// Creates an instance using the default value and assigns it to a mutable variable
83+
// Creates an instance using the default value
84+
// and assigns it to a mutable variable
8385
var person1 = Person()
8486

85-
// Creates an instance by passing a specific value and assigns it to a read-only variable
87+
// Creates an instance by passing a specific value
88+
// and assigns it to a read-only variable
8689
val person2 = Person("Joe")
8790
```
8891

@@ -94,7 +97,8 @@ It also demonstrates
9497
how to create an instance with both the default constructor's value and a specific value:
9598

9699
```kotlin
97-
// Class header with primary constructor that initializes name with a default value
100+
// Class header with a primary constructor
101+
// that initializes name with a default value
98102
class Person(val name: String = "Sebastian")
99103

100104
fun main() {
@@ -151,14 +155,14 @@ class Person(name: String) { /*...*/ }
151155
```
152156

153157
The primary constructor can declare parameters as properties. Use the `val` keyword before the argument name to declare a read-only property
154-
and the `var` keyword for a mutable property.
158+
and the `var` keyword for a mutable property:
155159

156160
```kotlin
157161
class Person(val name: String, var age: Int) { /*...*/ }
158162
```
159163

160164
These constructor parameter properties are stored as part of the instance and are accessible from outside the class.
161-
It's also possible to declare primary constructor parameters only, not stored in the instance, and available only within the class:
165+
It's also possible to declare primary constructor parameters only, not stored in the instance, and available only within the class body:
162166

163167
```kotlin
164168
// Primary constructor parameter that is also a property
@@ -170,8 +174,11 @@ class Person2(val name: String) {
170174

171175
// Primary constructor parameter only (not stored as a property)
172176
class Person1(name: String) {
177+
// Must be assigned to a property to be usable later
178+
val n: String = name
179+
173180
fun greet() {
174-
println("Hello, $name")
181+
println("Hello, $n")
175182
}
176183
}
177184
```
@@ -198,7 +205,8 @@ class Person(val name: String = "John", var age: Int = 30) { /*...*/ }
198205
If no value is passed during [instance creation](#creating-instances-of-classes), the property uses the default value:
199206

200207
```kotlin
201-
// Class with a primary constructor including default values for name and age
208+
// Class with a primary constructor
209+
// including default values for name and age
202210
class Person(val name: String = "John", var age: Int = 30)
203211

204212
fun main() {
@@ -213,12 +221,14 @@ fun main() {
213221
Use the primary constructor parameters to initialize additional class properties directly in the class body:
214222

215223
```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
217226
class Person(
218227
val name: String = "John",
219228
var age: Int = 30
220229
) {
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
222232
val description: String = "Name: $name, Age: $age"
223233
}
224234

@@ -232,7 +242,7 @@ fun main() {
232242
```
233243
{kotlin-runnable="true"}
234244

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:
236246

237247
```kotlin
238248
class Person(
@@ -244,14 +254,16 @@ class Person(
244254

245255
### Initializer blocks
246256

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.
249262

250263
Declare initializer blocks with the `init` keyword followed by curly braces `{}`.
251264
Write within the curly braces any code that you want to run during initialization:
252265

253266
```kotlin
254-
//sampleStart
255267
// Class with a primary constructor that initializes name and age
256268
class Person(val name: String, var age: Int) {
257269
init {
@@ -265,7 +277,6 @@ fun main() {
265277
// Creates an instance of the Person class
266278
Person("John", 30)
267279
}
268-
//sampleEnd
269280
```
270281
{kotlin-runnable="true"}
271282

@@ -306,6 +317,16 @@ fun main() {
306317
You can use the primary constructor parameters in the initializer blocks. For example, in the code above, the first and second initializers use
307318
the `name` and `age` parameters from the primary constructor.
308319

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+
class Person(val age: Int) {
324+
init {
325+
require(age > 0, "age must be positive")
326+
}
327+
}
328+
```
329+
309330
### Secondary constructors
310331

311332
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
321342
```kotlin
322343
// Class header with a primary constructor that initializes name and age
323344
class Person(val name: String, var age: Int) {
324-
// Secondary constructor that takes a String age and converts it to an integer
345+
346+
// Secondary constructor that takes a String age
347+
// and converts it to an integer
325348
constructor(name: String, age: String) : this(name, age.toIntOrNull() ?: 0) {
326349
println("$name created with converted age: $age")
327350
// Bob created with converted age: 8
@@ -340,7 +363,7 @@ fun main() {
340363
{style="tip"}
341364

342365
In the code above, the secondary constructor delegates to the primary constructor via the `this` keyword,
343-
passing `name` and the default `age` value.
366+
passing `name` and the `age` value converted to an integer.
344367

345368
In Kotlin, secondary constructors must delegate to the primary constructor. This delegation ensures that all primary
346369
constructor initialization logic is executed before any secondary constructor logic runs.
@@ -357,14 +380,16 @@ class Person(
357380
val name: String,
358381
var age: Int
359382
) {
360-
// Secondary constructor with direct delegation to the primary constructor
383+
// Secondary constructor with direct delegation
384+
// to the primary constructor
361385
constructor(name: String) : this(name, 0) {
362386
println("Person created with default age: $age and name: $name.")
363387
// Person created with default age: 0 and name: Alice.
364388
// Person created with default age: 0 and name: Bob.
365389
}
366390

367-
// Secondary constructor with indirect delegation: this("Bob") -> constructor(name: String) -> primary constructor
391+
// Secondary constructor with indirect delegation:
392+
// this("Bob") -> constructor(name: String) -> primary constructor
368393
constructor() : this("Bob") {
369394
println("New person created with default age: $age and name: $name.")
370395
// New person created with default age: 0 and name: Bob.
@@ -421,7 +446,8 @@ class Person {
421446
}
422447

423448
fun main() {
424-
// Creates an instance of the Person class using the implicit primary constructor
449+
// Creates an instance of the Person class
450+
// using the implicit primary constructor
425451
val person = Person()
426452
}
427453
```
@@ -486,7 +512,8 @@ abstract class Person(
486512
val name: String,
487513
val age: Int
488514
) {
489-
// Abstract member (doesn't provide an implementation, must be implemented by subclasses)
515+
// Abstract member
516+
// Doesn't provide implementation, must be implemented by subclasses
490517
abstract fun introduce()
491518

492519
// Non-abstract member (has an implementation)

0 commit comments

Comments
 (0)