Switch to full style
General Java code examples
Post a reply

Define Enum inside a class

Sat Aug 29, 2009 12:10 pm

Defined Enum inside a class
Take in mind that we deal with enum inside a class , like others members .
Code:
public class EnumTest {
     
enum Food {
    
BREAD("BREAD"),CHEESE("CHEESE"),MEAT("MEAT");
    private 
String myfood;
    
Food(String s)
    {
      
myfood ;
    }
  public 
String getValue()
  {
      
      return 
myfood ;
  }
     }

 
    public static 
void main(String args[])
    {
         
System.out.println(Food.BREAD.myfood+"  "+Food.BREAD+" "+Food.values());
         new 
EnumTest2();
    }
}
class 
EnumTest2 {

    
EnumTest2()
    {
        
System.out.println(EnumTest.Food.BREAD);
    }
}
 


As you see the difference , you can access the Enum directly inside a static function .

The output is :
Code:
BREAD  BREAD [LEnumTest$Food;@11b86e7
BREAD





Post a reply
  Related Posts  to : Define Enum inside a class
 Define class inside another class C++     -  
 Define enum in java     -  
 Define class helper class to check the method existance     -  
 braces block inside your class     -  
 define final class     -  
 define a Class with a Method     -  
 Define your own exception class     -  
 how to Define abstract class in php     -  
 Define getter and setter in php class     -  
 Define boolean Class properties     -  

Topic Tags

Java OOP