
- 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
How to check if a "lateInit" variable has been initialized in Kotlin?
Any variable which is initialized after its declaration is known as a "late initialized variable". In conventional programming languages, any non-NULL type of variable need to be initialized in the constructor. But sometimes, by mistake, developers forget to do these NULL checks which causes a programming error. In order to avoid this situation, Kotlin introduced a new modifier called as "lateInit". Along with this modifier, Kotlin provides a couple of methods to check whether this variable is initialized or not.
In order to create a "lateInit" variable, we just need to add the keyword "lateInit" as an access modifier of that variable. Following are the couple of conditions that need to be followed in order to use "lateInit" in Kotlin −
Use "lateInit" with a mutable variable. That means, we need use "var" keyword with "lateInit".
"lateInit" is only allowed with non-NULLable data types.
"lateInit" does not work with primitive data types.
"lateInit" can be used when the variable property does not have any getter and setter methods.
Example
In this example, we will declare a variable as a "lateInit" variable and we will be using our Kotlin library function to check whether the variable is initialized or not.
class Tutorial { lateinit var name : String fun checkLateInit(){ println(this::name.isInitialized) // it will print false as the value is not initialized // initializing name name = "www.tutorialspoint.com/" println(this::name.isInitialized) // It will return true } } fun main() { var obj=Tutorial(); obj.checkLateInit(); }
Output
Once you execute the code, it will generate the following output −
false true
In the second case, the variable name is initialized, hence it returns True.
- Related Articles
- How to check if a variable has a numeric value in Perl?
- C++ Program to check if a string has been seen before
- How to check if a variable exists in JavaScript?
- How to check which key has been pressed using jQuery?
- Kotlin – Property initialization using "by lazy" vs. "lateinit"
- How to check if a string is empty in Kotlin?
- How to check if a variable is NaN in JavaScript?
- How to check if a variable is boolean in JavaScript?
- How to check if a variable is an integer in JavaScript?
- How to check if a variable is an array in JavaScript?
- How to check if a variable is NULL in C/C++?
- How to determine if an activity has been called by a Notification in Android?
- How to check if a date has passed in MySQL?
- How to Check if Android EditText is empty in Kotlin?
- How to check if type of a variable is string in Python?
