
- 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
Kotlin – Property initialization using "by lazy" vs. "lateinit"
Kotlin library provides two different access modifiers for property declaration.
In this article, we will highlight the difference between these two access modifiers and how we can use them in our application.
Lateinit
In order to create a "lateInit" variable, we just need to add the keyword "lateInit" as an access modifier of that variable. Following are a set of conditions that need to be followed in order to use "lateInit" in Kotlin.
Use "lateInit" with a mutable variable. That means, we need to use "var" keyword with "lateInit".
"lateInit" is allowed only 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
We will be declaring a variable as lateInit and we will see how we can access the same throughout the program.
class Tutorial { lateinit var name : String fun checkLateInit(){ // checking whether the value is assigned or not if(this::name.isInitialized) println("Your value is not assigned"); else{ // initializing name name = "www.tutorialspoint.com/" println(this.name) // this will return true } } } fun main() { var obj=Tutorial(); obj.checkLateInit(); }
Output
Once you execute the code, it will generate the following output −
www.tutorialspoint.com/
Lazy Initialization
For efficient memory management, Kotlin has introduced a new feature called as Lazy initialization. When the lazy keyword is used, the object will be created only when it is called, otherwise there will be no object creation. lazy() is a function that takes a lambda and returns an instance of lazy which can serve as a delegate of lazy properties upon which it has been applied. It has been designed to prevent unnecessary initialization of objects.
Lazy can be used only with non-NULLable variables.
Variable can only be val. "var" is not allowed .
Object will be initialized only once. Thereafter, you receive the value from the cache memory.
The object will not be initialized until it has been used in the application.
Example
In this example, we will declare a lazy variable "myName" and we could see that the call to this parts of the code will happen only once and when the value is initialized, it will remember the value throughout the application. Once the value is assigned using lazy initialization, it cannot be reassigned .
class Demo { val myName: String by lazy { println("Welcome to Lazy declaration"); "www.tutorialspoint.com" } } fun main() { var obj=Demo(); println(obj.myName); println("Second time call to the same object--"+obj.myName); }
Output
Once you execute the code, it will generate the following output −
Welcome to Lazy declaration www.tutorialspoint.com Second time call to the same object--www.tutorialspoint.com
- Related Articles
- Initialization vs Instantiation in C#
- How to check if a "lateInit" variable has been initialized in Kotlin?
- IntArray vs. Array in Kotlin
- What Is Lazy Loading? Understanding Lazy Loading for SEO
- Lazy loading of images in table view using Swift
- CSS2 sizing property vs CSS3 box sizing property
- Kotlin vs Java, Which One is the Best?
- Lazy Loading in ReactJS
- Lazy Stored Procedure in Swift
- What do you mean by a dynamic initialization of variables?
- Kotlin 'when' statement vs Java 'switch'
- How to Enjoy a Lazy Day?
- How do I load an ImageView by URL on Android using kotlin?
- Find the product by using distributive property : $1009 \times1392$
- Is there a difference between copy initialization and direct initialization in C++?
