0
0
Kotlinprogramming~15 mins

Object declaration syntax in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Object Declaration Syntax in Kotlin
📖 Scenario: You are creating a simple Kotlin program to manage a single configuration object for an app. This object will hold some settings that the app uses.
🎯 Goal: Build a Kotlin program that declares an object using Kotlin's object declaration syntax. You will create the object, add a property, add a function, and then print the result of calling that function.
📋 What You'll Learn
Declare an object called AppConfig
Inside AppConfig, create a property called version with value "1.0.0"
Inside AppConfig, create a function called getInfo that returns a string combining the text "App version: " and the version property
Print the result of calling AppConfig.getInfo()
💡 Why This Matters
🌍 Real World
Kotlin objects are used to create single instances that hold configuration, utility functions, or shared data in Android apps and Kotlin programs.
💼 Career
Understanding object declaration syntax is important for Kotlin developers to write clean, organized code and manage shared resources effectively.
Progress0 / 4 steps
1
Declare the object AppConfig
Write a Kotlin object declaration named AppConfig with no properties or functions inside.
Kotlin
Need a hint?

Use the object keyword followed by the name AppConfig and curly braces.

2
Add a property version to AppConfig
Inside the AppConfig object, add a val property called version and set it to the string "1.0.0".
Kotlin
Need a hint?

Use val version = "1.0.0" inside the object.

3
Add a function getInfo to AppConfig
Inside the AppConfig object, add a function called getInfo that returns a string combining "App version: " and the version property using string templates.
Kotlin
Need a hint?

Define fun getInfo(): String and return the string using "App version: $version".

4
Print the result of AppConfig.getInfo()
Write a println statement that prints the result of calling AppConfig.getInfo().
Kotlin
Need a hint?

Use println(AppConfig.getInfo()) inside a main function.