

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Getters and Setters in Kotlin
Properties in Kotlin can be declared either as mutable using the "var" keyword or as read-only using the "val" keyword. Both these types of variables can be referred by their respective names after the method declaration.
In Kotlin, getter() and setter() methods need not be created explicitly. The Kotlin library provides both of them by default.
Example
In this example, we will see how to use the getter() and setter() methods in Kotlin.
fun main(args: Array<String>) { // getter() println("Name is -> " + Address().name) println("City is -> " + Address().city) println("State is -> " + Address().state) } class Address { // setter() var name: String = "www.tutorialspoint.com" var city: String = "Hyderabad" val state: String = "Telangana" }
Output
Once executed, it will yield the following output.
Name is -> www.tutorialspoint.com City is -> Hyderabad State is -> Telangana
- Related Questions & Answers
- getters and setters in JavaScript classes?
- What are getters and setters methods in PHP?
- What is the difference between Getters and Setters in JavaScript?
- What are Getters/Setters methods for Python Class?
- Smart / self-overwriting / lazy getters in javaScript?
- Kotlin static methods and variables
- Difference between isNullOrEmpty and isNullOrBlank in Kotlin
- Difference between List and Array types in Kotlin
- Add and Remove Views in Android Dynamically in Kotlin?
- Difference between Java and Kotlin in Android with Examples
- Subscript and Superscript a String in Android using Kotlin.
- Android imageView Zoom-in and Zoom-Out using Kotlin?
- What are constants in Kotlin and how to create them?
- @Throws Annotation in Kotlin
- 'break' and 'continue' in forEach in Kotlin
Advertisements