Properties with val and var
📖 Scenario: Imagine you are creating a simple app to store information about a book. Each book has a title and a number of pages.
🎯 Goal: You will create a Kotlin class called Book with properties using val and var. Then you will create an instance of this class and print its details.
📋 What You'll Learn
Create a class named
Book with two properties: title and pages.Make
title a val property (cannot be changed after creation).Make
pages a var property (can be changed after creation).Create an instance of
Book with the title "Kotlin Basics" and 200 pages.Change the number of pages to 250.
Print the book's title and pages.
💡 Why This Matters
🌍 Real World
Understanding <code>val</code> and <code>var</code> is essential for managing data that should or should not change in apps, like user profiles or settings.
💼 Career
Many Kotlin jobs require knowledge of properties to write safe and clear code, especially in Android app development.
Progress0 / 4 steps