- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Articles
- Getters and setters in JavaScript classes?
- What are getters and setters methods in PHP?
- How to use getters and setters in TypeScript?
- What are Getters/Setters methods for Python Class?
- Smart / self-overwriting / lazy getters in javaScript?
- Difference between "fold" and "reduce" in Kotlin
- Difference between "*" and "Any" in Kotlin generics
- Difference between isNullOrEmpty and isNullOrBlank in Kotlin
- Kotlin static methods and variables
- Difference between List and Array types in Kotlin
- Equality checks in Kotlin (Difference between "==" and "===" Operators)
- Add and Remove Views in Android Dynamically in Kotlin?
- Difference between Java and Kotlin in Android with Examples
- Android imageView Zoom-in and Zoom-Out using Kotlin?
- Subscript and Superscript a String in Android using Kotlin.

Advertisements