What is enum? Why should we use? Any example?

What is enum? Why should we use? Any example?

Postby Kishan » Tue Feb 05, 2013 7:45 am

What is enum? Why should we use? Any example?
Kishan
 
Posts: 191
Joined: Tue Jan 29, 2013 4:21 am

Re: What is enum? Why should we use? Any example?

Postby javalatte » Fri Feb 22, 2013 3:55 am

enum is a special data type. The enum declaration defines a class of type enum.

Whenever you want to define a data type, that could only have a fixed set of constants as values, you use a enum.

For example, days of a week, colors of a rainbow, sizes of shirt, etc could be defined as enum data types.

public enum Size { SMALL, MEDIUM, LARGE, XLARGE }

//Accessed as,
Size.SMALL, Size.MEDIUM, Size.LARGE, Size.XLARGE
//Declared as,
Size mySize = Size.MEDIUM;
javalatte
 
Posts: 55
Joined: Tue Jan 29, 2013 2:31 pm


Return to Core Java

Who is online

Users browsing this forum: No registered users and 1 guest

cron