Enumuration

Enumuration:


  • The constant in enum are implicity final(i.e the contant defined once can't be be changed and it defined by default(implicitly))
  • The contant in  enum are also implicitly static.(i.e the 

Points:

  • It can't be instantiate i.e using new you can't make an object of enum class.
  • It can't be inherited from another class.
  • An enum can't be a superclass.

Rationship between enum types and classes. Like classes, all
enum types are reference types. An enum type is declared with an enum declaration, which
is a comma-separated list of enum constants—the declaration may optionally include other
components of traditional classes, such as constructors, fields and methods

source:
JavaCompleteReference Pg#261

Examples:

class DemoApple{

 public static void main (String args[])
 {
   Apple arr[]=Apple.values();
 
   for(Apple ap:arr)
   System.out.println(ap+" "+ap.getPrice());    
 }

}


enum Apple
{
    GoldenDel(9),RedDel(3),Winsap(15); 

 private int price;
 Apple(int a)
 {
  this.price=a;
 }
public int getPrice()
  {
   return price;
  }
 
}

0 comments:

Post a Comment

My Instagram