- 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
Creating POJO Class for Kotlin
Kotlin has been developed over JVM and hence it is fully compatible with JVM. Java POJO class stands for Plain Old Java Object (POJO) which is used to hold the data.
In Java, along with defining the variables, we need to create different supporting methods in order to access those private members of the class.
But Kotlin provides a unique way to declare a POJO class by introducing the "data" keyword. It can be applied along with class.
Once a class is defined as data class, the Kotlin compiler itself will be creating all the supporting getter() and setter() methods for this class.
Example – POJO using data class
In this example, we will see how we can create a POJO class of Student and how we can access the same from our main().
fun main(args: Array<String>) { // object initialization using default constructor val stud = student("name","location") //default getter() will be called println(stud.name) println(stud.loc) } // creating data class data class student (val name: String,val loc: String)
Output
Once the above piece of code is executed, it will generate the following output −
name location
- Related Articles
- Override getter for Kotlin data class
- POJO vs Java Beans
- Extend data class in Kotlin
- Creating a chained operation class in JavaScript
- How to check "instanceof" class in Kotlin?
- Creating an empty case-sensitive HybridDictionary Class in C#
- How to use the TextWatcher class in kotlin?
- Difference between a "class" and an "object" in Kotlin
- Convert POJO to XML using the Jackson library in Java?
- Convert XML to POJO using the Jackson library in Java?
- Python Functions creating iterators for efficient looping
- How to implement lambda expression without creating an anonymous class in Java?
- How to create an instance of an abstract class in Kotlin?
- Vagrant vs Docker for creating an isolated environment
- Tips for creating the best YouTube channel name
