Universitas Kebudayaan Digital Makassar

Universitas Kebudayaan Digital Makassar (UKDMKS) memadukan inovasi teknologi digital dengan pelestarian budaya lokal. Bergabunglah untuk membangun masa depan berbasis budaya dan teknologi!

android kotlin pemograman

Apa itu Enumeration di Kotlin

Enumeration merupakan salah fitur yang bisa kita gunakan untuk menyimpan kumpulan objek yang telah didefinisikan menjadi tipe data konstanta. 

Enumeration dapat ditetapkan sebagai nilai ke dalam sebuah variabel dengan cara yang lebih efisien. Selain itu, Enumeration juga dapat digunakan untuk meminimalisir kesalahan dalam pengetikan nilai sebuah variabel, misalnya:


  1. val colorRed = 0xFF0000

  2. val colorGreen = 0x00FF00

  3. val colorBlue = 0x00FF00


Nilai dari beberapa variabel di atas berpotensi salah atau tertukar dengan nilai variabel lain. Untuk meminimalisir kesalahan, kita memerlukan Enumeration. Anda bisa melakukannya seperti ini: 


  1. fun main() {

  2.     val colorRed = Color.RED

  3.     val colorGreen = Color.GREEN

  4.     val colorBlue = Color.BLUE

  5. }

  6.  

  7. enum class Color(val value: Int) {

  8.     RED(0xFF0000),

  9.     GREEN(0x00FF00),

  10.     BLUE(0x0000FF)

  11. }


Selain meminimalisir terjadinya kesalahan pengetikan, Enumeration juga membuat kode yang kita tulis jadi lebih bersih dan mudah dibaca.
Untuk mendefinisikan sebuah kelas Enum, kita bisa menggunakan kata kunci enum dan setiap objek yang berada di dalamnya dipisahkan oleh karakter koma (,). Selain itu, objek yang berada di dalam Enum secara implisit bersifat static dan final sehingga kita tidak dapat mengubahnya setelah dideklarasikan. Berikut contoh implementasi yang paling mendasar dari sebuah Enum:


  1. enum class Color{

  2.     RED, GREEN, BLUE

  3. }


Objek yang telah didefinisikan menjadi tipe data Enum dapat mengakses attribute atau method di dalam kelas Enum itu sendiri. Konsep ini sama halnya seperti Array. Bedanya, Enum berbentuk constant. Berikut contoh ketika kita ingin mengakses objek yang berada di dalam kelas Enum:


  1. fun main() {

  2.     val color : Color = Color.RED

  3.     print(color)

  4. }

  5.  

  6. enum class Color{

  7.     RED, GREEN, BLUE

  8. }

  9.  

  10. /*

  11.    output: RED

  12. */


Seperti yang dicontohkan di awal, setiap objek yang dideklarasikan merupakan instance dari kelas Enum tersebut. Kita bisa menginisialisasinya seperti berikut:


  1. enum class Color(val value: Int) {

  2.     RED(0xFF0000),

  3.     GREEN(0x00FF00),

  4.     BLUE(0x0000FF)

  5. }


Selain itu, kita juga dapat mendeklarasikan anonymous class untuk setiap objek Enum, misalnya: 


  1. enum class Color(val value: Int) {

  2.     RED(0xFF0000){

  3.         override fun printValue() {

  4.             println("value of RED is $value")

  5.         }

  6.     },

  7.     GREEN(0x00FF00){

  8.         override fun printValue() {

  9.             println("value of GREEN is $value")

  10.         }

  11.     },

  12.     BLUE(0x0000FF){

  13.         override fun printValue() {

  14.             println("value of BLUE is $value")

  15.         }

  16.     };

  17.  

  18.     abstract fun printValue()

  19. }


Setiap enum class memiliki sebuah synthetic method yang memungkinkan kita mendapatkan daftar objek Enum dan nama dari tiap Enum itu sendiri.


  1. fun main() {

  2.     val colors: Array<Color> = Color.values()

  3.     colors.forEach { color ->

  4.         print(color)

  5.     }

  6. }

  7.  

  8. enum class Color(val value: Int) {

  9.     RED(0xFF0000),

  10.     GREEN(0x00FF00),

  11.     BLUE(0x0000FF)

  12. }

  13.  

  14. /*

  15.    output : RED, GREEN, BLUE

  16. */

  17.  

  18.  


Perhatikan. Untuk mendapatkan daftar objek Enum kita bisa menggunakan fungsi values(). Sedangkan untuk mendapatkan nama dari objek Enum kita bisa menggunakan fungsi valueOf() seperti berikut:


  1. fun main() {

  2.     val color: Color = Color.valueOf("RED")

  3.     print("Color is $color")

  4. }

  5.  

  6. enum class Color(val value: Int) {

  7.     RED(0xFF0000),

  8.     GREEN(0x00FF00),

  9.     BLUE(0x0000FF)

  10. }

  11.  

  12. /*

  13.    output : Color is RED

  14. */


Saat menggunakan fungsi valueOf() , perhatikan argumen yang kita masukkan ke dalam fungsi tersebut. Jika argumen yang kita masukan tidak sama dengan salah satu objek enum maka akan terjadi kesalahan IllegalArgumentException.
Selain menggunakan fungsi values() dan fungsi valueOf(), kita bisa mendapatkan daftar objek Enum dan nama dari objek Enum dengan cara yang lebih umum. Caranya, gunakan fungsi enumValues() dan fungsi enumValueOf(). Contoh penggunaan kedua fungsi tersebut adalah sebagai berikut:


  1. fun main() {

  2.     val colors: Array<Color> = enumValues()

  3.     colors.forEach {color ->

  4.         println(color)

  5.     }

  6.  

  7.     val color: Color = enumValueOf("RED")

  8.     println("Color is $color")

  9. }

  10.  

  11. enum class Color(val value: Int) {

  12.     RED(0xFF0000),

  13.     GREEN(0x00FF00),

  14.     BLUE(0x0000FF)

  15. }

  16.  

  17. /*

  18.    output :

  19.        RED

  20.        GREEN

  21.        BLUE

  22.        Color is RED

  23.  

  24. */


Konsep dari Enumeration sendiri sama seperti Array. Oleh karena itu, selain mendapatkan daftar dan nama dari tiap objek Enum, kita juga bisa mendapatkan posisi tiap objek menggunakan properti ordinal seperti berikut:


  1. fun main() {

  2.     val color: Color = Color.GREEN

  3.  

  4.     print("Position GREEN is ${color.ordinal}")

  5. }

  6.  

  7. enum class Color(val value: Int) {

  8.     RED(0xFF0000),

  9.     GREEN(0x00FF00),

  10.     BLUE(0x0000FF)

  11. }

  12.  

  13. /*

  14.    output : Position GREEN is 1

  15.  

  16. */


Di atas telah disebutkan bahwa setiap objek merupakan instance dari enum class yang kita definisikan. Lantas bagaimana cara kita mengecek instance dari Enum itu sendiri? Nah, untuk mengeceknya, gunakan When Expression seperti berikut:


  1. fun main() {

  2.     val color: Color = Color.GREEN

  3.  

  4.     when(color){

  5.         Color.RED -> print("Color is Red")

  6.         Color.BLUE -> print("Color is Blue")

  7.         Color.GREEN -> print("Color is Green")

  8.     }

  9. }

  10.  

  11. enum class Color(val value: Int) {

  12.     RED(0xFF0000),

  13.     GREEN(0x00FF00),

  14.     BLUE(0x0000FF)

  15. }

  16.  

  17. /*

  18.    output : Color is Green

  19.  

  20. */


Ketika menggunakan when untuk mengecek instance dari Enum, lebih baik masukkan setiap objek Enum yang kita definisikan. Jika kita melewatkan salah satu objek,  peringatan berikut akan muncul: ‘when’ expression on enum is recommended to be exhaustive
Asalas | Unlock Anime, Manhwa & Donghua Like Never BeforeFinasteriden: Unlock the Secrets of the Movie World!Marians Woman: Celebrating Beauty Around the WorldArheon - Unveiling the Secrets of Food & Origins WorldwideMPAPER The Ultimate Destination for Anime & Manga FansANMC21: Your Guide to Smarter Living and SuccessMV Agusta of Tampa: Your Automotive News HubTech and Play: Your Hub for Technology, Gaming, and GadgetsRebeccaSommer : Stories & Cultures from Around the WorldUETRABAJANDOJUNTOS - Inside the Music World’s Best-Kept SecretsVandelay Armor - Viral News and Global UpdatesGainesville Sun: Technology & Computers UnveiledGRFX Gaming Party Bus: Journey Through Gaming ErasHouse of Beauty: Celebrating the World's Most Beautiful WomenLearn Mistake: Wisdom for a Better LifeSports Hope Alive: Portal Olahraga DuniaWorld News TW - The Hottest Viral News & Global HeadlinesWriter Sujatha - Life Lessons & Struggles That Inspirehttps://128.199.185.186/https://143.198.89.74/https://165.227.47.178/https://170.64.208.214/https://170.64.192.239/https://46.101.102.216/LVONLINEtelegram lvonlinehttps://www.thecarbongenie.com/https://www.aievea-bijou.com/https://www.slashpolicy.com/https://www.benwestbeech.com/https://www.hh-bags.com/https://www.drupalforfacebook.org/https://www.lvonline.boats/https://www.lvoslot.com/https://www.lvobet.id/https://www.lvoslot.id/https://www.lvonline000.com/https://www.lvonline001.com/https://www.lvonline002.com/https://www.lvonline003.com/https://www.lvonline004.com/https://www.lvonline005.com/https://www.lvonline006.com/https://www.lvonline007.com/https://www.lvonline008.com/https://www.lvonline009.com/https://www.lvonline010.com/https://www.lvonlinepoker.com/https://www.lvonlinebola.com/https://www.lvonlinekasino.com/https://www.lvonline.io/https://www.lvonline.store/https://www.lvonline.online/https://www.situslvonline.us/situs bandar lvonlinehttps://146.190.97.83/https://143.198.209.134/https://188.166.246.204/https://167.172.83.179/https://togelhok.tv/https://www.togelhok.id/https://earthtoweb.com/https://www.elearningfacultymodules.org/https://www.how6youtoknowc.org/https://128.199.71.129/https://167.172.64.185/https://152.42.192.250/https://www.capcut88.com/https://www.capcut88.co/https://towsonsmiles.com/https://www.campur88.com/https://www.campur88.org/https://www.campur88.work/https://www.campur88.xyz/https://www.campur88.lol/https://www.nagacampur.biz/https://www.nagacampur.club/https://www.nagacampur.co/https://www.nagacampur.me/https://www.nagacampur.xyz/https://www.nasicampur88.com/https://165.232.175.185/https://152.42.164.228/https://152.42.194.203/https://152.42.169.214/https://www.campurslot.com/https://www.campurslot.id/https://www.campurslot.co/https://www.campurslot.org/https://www.campurslot.homes/https://www.campurslot.design/Badan Lembaga Pendidikan Ilmu Komputer SubangUniversitas Negeri JeparaLembaga Universitas Kristen MaranthaSMP 3 Negri Nganjukhttps://iklanmalay.com/https://promobola.comhttps://kapsychologists.comhttps://propertycloudsite.comSattar777 NewsVR Slot Online NewsRuby888 Online Slot NewsBerita Global Klik Agenslot228Agen Sloto SG777 NewsGold Club SlotOnline NewsAzar Marra Kech NewsBerita Sidney dan MancanegaraBerita Seputar Sumsel dan DuniaBerita Rehabilitasi Judi OnlineBerita Link Gacor TerupdateItalian Tuition News UpdateWPJS Online NewsBerita Agen Slot RoyalThai Slot Online NewsAll Online Game NewsOnline Game News UpdateAsian Lotre NewsBerita Demo Dana SlotBerita Kalbar ProvLocanda della Maria NewsWye Valley NewsBerita Agen Slot1004Berita Agen Slot33Agen Slot399 NewsPlayboy Slot Online NewsSlot Online BB Slot NewsSlot Online 911 News365 Slot Online NewsEat Atlah Newsambamali canadaInfo Seputar Sepakbolacentre thoughtBerita Hiburanopen etherpadras indo groupresistance manualPrediksi Shiowe want real newsthe poisoned pawnindonesia reclaimed teakswift kennedy and copullip passionmy passion foraim torontoPublic FlashesFriweb TeknologiIngenious Gamersthe late show gardensGishPuppy Newsslot danaOregon Farm Garden NewsViral Pulse GlobaljumpajpPromo Bola soccer Captivates The WorldKapsychologists World First The Science of Mental Health - Understanding Psychiatry: The Science of Mental HealthPropertyCloudSite How to Make Smarter Investments in Today’s MarketArnavichara Ultimate Guide Right Business SoftwareAuscare Disability A Comprehensive Guide to Retirement Homes Finding the Perfect Place to Enjoy Your Golden YearsSeries Mp4 The Future of Entertainment Streaming and Downloadable Video ExplainedAlogirlxinh How to Create a Successful Personal Page or Blog in 2024ihokibethttps://bengbulang-karangpucung.cilacapkab.go.id/https://comunicacion.unsa.edu.ar/https://seychellesbiodiversitychm.sc/https://www.925lms.com/https://www.guisseny.memoire.bzh/https://www.mobiliars.org/https://www.squashparkwieliczka.pl/
Asalas | Unlock Anime, Manhwa & Donghua Like Never BeforeFinasteriden: Unlock the Secrets of the Movie World!Marians Woman: Celebrating Beauty Around the WorldArheon - Unveiling the Secrets of Food & Origins WorldwideMPAPER The Ultimate Destination for Anime & Manga FansANMC21: Your Guide to Smarter Living and SuccessMV Agusta of Tampa: Your Automotive News HubTech and Play: Your Hub for Technology, Gaming, and GadgetsRebeccaSommer : Stories & Cultures from Around the WorldUETRABAJANDOJUNTOS - Inside the Music World’s Best-Kept SecretsVandelay Armor - Viral News and Global UpdatesGainesville Sun: Technology & Computers UnveiledGRFX Gaming Party Bus: Journey Through Gaming ErasHouse of Beauty: Celebrating the World's Most Beautiful WomenLearn Mistake: Wisdom for a Better LifeSports Hope Alive: Portal Olahraga DuniaWorld News TW - The Hottest Viral News & Global HeadlinesWriter Sujatha - Life Lessons & Struggles That Inspirehttps://128.199.185.186/https://143.198.89.74/https://165.227.47.178/https://170.64.208.214/https://170.64.192.239/https://46.101.102.216/LVONLINEtelegram lvonlinehttps://www.thecarbongenie.com/https://www.aievea-bijou.com/https://www.slashpolicy.com/https://www.benwestbeech.com/https://www.hh-bags.com/https://www.lvonline.boats/https://www.lvoslot.com/https://www.lvobet.id/https://www.lvoslot.id/https://www.lvonline000.com/https://www.lvonline001.com/https://www.lvonline002.com/https://www.lvonline003.com/https://www.lvonline004.com/https://www.lvonline005.com/https://www.lvonline006.com/https://www.lvonline007.com/https://www.lvonline008.com/https://www.lvonline009.com/https://www.lvonline010.com/https://www.lvonlinepoker.com/https://www.lvonlinebola.com/https://www.lvonlinekasino.com/https://www.lvonline.io/https://www.lvonline.store/https://www.lvonline.online/https://www.situslvonline.us/situs bandar lvonlinehttps://146.190.97.83/https://143.198.209.134/https://188.166.246.204/https://167.172.83.179/https://togelhok.tv/https://www.togelhok.id/https://earthtoweb.com/https://www.elearningfacultymodules.org/https://www.how6youtoknowc.org/https://128.199.71.129/https://167.172.64.185/https://152.42.192.250/https://www.capcut88.com/https://www.capcut88.co/https://towsonsmiles.com/https://www.campur88.com/https://www.campur88.org/https://www.campur88.work/https://www.campur88.xyz/https://www.campur88.lol/https://www.nagacampur.biz/https://www.nagacampur.club/https://www.nagacampur.co/https://www.nagacampur.me/https://www.nagacampur.xyz/https://www.nasicampur88.com/https://165.232.175.185/https://152.42.164.228/https://152.42.194.203/https://152.42.169.214/https://www.campurslot.com/https://www.campurslot.id/https://www.campurslot.co/https://www.campurslot.org/https://www.campurslot.homes/https://www.campurslot.design/Badan Lembaga Pendidikan Ilmu Komputer SubangUniversitas Negeri JeparaLembaga Universitas Kristen MaranthaSMP 3 Negri Nganjukhttps://iklanmalay.com/https://promobola.comhttps://kapsychologists.comhttps://propertycloudsite.comSattar777 NewsVR Slot Online NewsRuby888 Online Slot NewsBerita Global Klik Agenslot228Agen Sloto SG777 NewsGold Club SlotOnline NewsAzar Marra Kech NewsBerita Sidney dan MancanegaraBerita Seputar Sumsel dan DuniaBerita Rehabilitasi Judi OnlineBerita Link Gacor TerupdateItalian Tuition News UpdateWPJS Online NewsBerita Agen Slot RoyalThai Slot Online NewsAll Online Game NewsOnline Game News UpdateAsian Lotre NewsBerita Demo Dana SlotBerita Kalbar ProvLocanda della Maria NewsWye Valley NewsBerita Agen Slot1004Berita Agen Slot33Agen Slot399 NewsPlayboy Slot Online NewsSlot Online BB Slot NewsSlot Online 911 News365 Slot Online NewsEat Atlah Newsambamali canadaInfo Seputar Sepakbolacentre thoughtBerita Hiburanopen etherpadras indo groupresistance manualPrediksi Shiowe want real newsthe poisoned pawnindonesia reclaimed teakswift kennedy and copullip passionmy passion foraim torontoPublic FlashesFriweb TeknologiIngenious Gamersthe late show gardensGishPuppy Newsslot danaOregon Farm Garden NewsViral Pulse GlobaljumpajpPromo Bola soccer Captivates The WorldKapsychologists World First The Science of Mental Health - Understanding Psychiatry: The Science of Mental HealthPropertyCloudSite How to Make Smarter Investments in Today’s MarketArnavichara Ultimate Guide Right Business SoftwareAuscare Disability A Comprehensive Guide to Retirement Homes Finding the Perfect Place to Enjoy Your Golden YearsSeries Mp4 The Future of Entertainment Streaming and Downloadable Video ExplainedAlogirlxinh How to Create a Successful Personal Page or Blog in 2024ihokibethttps://bengbulang-karangpucung.cilacapkab.go.id/https://comunicacion.unsa.edu.ar/https://seychellesbiodiversitychm.sc/https://www.925lms.com/https://www.guisseny.memoire.bzh/https://www.mobiliars.org/https://www.squashparkwieliczka.pl/https://mok.edu.kz/https://www.mware.cloud/https://www.facilitiescoolingandheating.com.au/https://www.krillpay.ng/https://925worksuite.com/