
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
What are constants in Kotlin and how to create them?
In every programming language, we need some variable whose value will never change thoroughout the program. In Kotlin too, we have a keyword to create such a variable whose value will remain as constant throughout the program. In order to declare a value as constant, we can use the "const" keyword at the beginning. In this article, we will learn how we can declare a variable as constant in a different way.
Example: Top level declaration
Example
Kotlin const variable can be declared at the top of the programming language and it can be used throughout the file scope.
private const val My_TOP_LEVEL_CONST_VAL = "Type 1--> Example of Top Level Constant Value" fun main() { println(My_TOP_LEVEL_CONST_VAL); }
Output
It will produce the following output −
Type 1--> Example of Top Level Constant Value
Example: Local constant
Like any other programming language, in Kotlin too, we can declare a local constant value and it will be blocked with in the specified scope. In the following example, we will create a local constant value.
Example
fun main() { val MY_LOCAL_CONST="Type 2-->Example of local const value" println(MY_LOCAL_CONST); }
Output
It will produce the following output −
Type 2-->Example of local const value
Example: Companion object const
Kotlin also provides an option to create a const function in the companion object. This is not recommended as per the recent programming architecture, because a companion object by default creates its own getter() and setter() methods which may cause performance issues.
Example
fun main() { println(Student.MY_CONSTANT); } class Student(){ companion object{ const val MY_CONSTANT = "Type 3--> Using companion Object" } }
Output
It will produce the following output −
Type 3--> Using companion Object
Example: Object declaration and direct call
A Constant variable can also be declared inside an object class. Later, this variable can be used by different means inside the program.
Example
fun main() { println(MyConstant.Const_USING_OBJECT_CLASS); } object MyConstant { const val Const_USING_OBJECT_CLASS = "Type 4-->Example of const using object class" }
Output
It will produce the following output −
Type 4-->Example of const using object class
- Related Articles
- What are sitemaps? How to create them?
- How to create Variables and Constants in C++?
- What are MySQL stored functions and how can we create them?
- What are constants in C++?
- How to create constants in JavaScript? Explain with examples.
- What are Enumerated Constants in C++?
- What are MySQL Temporary Tables? How can we create them?
- What are C++ Integer Constants?
- What are C++ Character Constants?
- What are Backlinks? And How to Build Them in 2023
- How to create a list in Kotlin?
- SPC Charts: Overview, When to Use Them, and How to Create Them
- What is a constant and how to define constants in Java?
- What HTML forms are and how to use them?
- What are lambda expressions and how to use them in Java?
