Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview



Download 7,31 Mb.
Pdf ko'rish
bet31/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   27   28   29   30   31   32   33   34   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Question 53: Explain enum
Answer: enum in java is a data type which contains a fixed set of
constants. In enum, we can also add variables, methods and
constructors. Some common examples of enums are: days of week,
colors, excel report columns etc.
Some points to remember:


enum constants are static and final implicitly
enum improves type safety
enum can be declared inside or outside of a class
enum can have fields, constructors (private) and
methods
enum cannot extend any class because it already
extends Enum class implicitly but it can implement many
interfaces
We can use enum in switch statement
We can have main() method inside an enum
enum has values(), ordinal() and valueOf() methods.
values() return an array containing all values present
inside enum, ordinal() method returns the index of given
enum value and valueOf() method returns the value of
given constant enum
enum can be traversed
enum can have abstract methods
enum cannot be instantiated because it contains private
constructor only
The constructor is executed for each enum constant at
the time of enum class loading
While defining enum, constants should be declared first,
prior to any fields or methods, or else compile time error
will come
Example 1:
main() method in enum, iterating over enum:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
53\Example 1\color.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
53\Example 1\output.png


Example 2:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
53\Example 2\color.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
53\Example 2\output.png



Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   27   28   29   30   31   32   33   34   ...   89




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish