WHAT IS ENUMERATIONS ?

enumeration is a list of named constants. It is created by enum keyword.
enum laptop{IBM,LENEVO,ACER,SONY,HP,HCL}
It's not required that enum constants be in all caps, but borrowing from the Sun
code convention that constants are named in caps, it's a good idea.

With this the compiler will stop you from assigning anything to a laplop except the contant values.
Enums can be declared as their own separate class, or as a class member,
however they must not be declared within a method!
DECLARING OUTSIDE A CLASS:

enum laptop{IBM,LENEVO,ACER,SONY,HP,HCL}
class computers{}
class Demo{choice=laptop.IBM;}

DECLARING INSIDE A CLASS:

class computers{
enum laptop{IBM,LENEVO,ACER,SONY,HP,HCL}
}
class Demo{choice=Demo.laptop.IBM;}

No comments:

Post a Comment