|
|
Program: How to call enum, which is defined inside a class?
Description: |
This example defines a basic enum type called Fruit inside a class. This example shows how to call enum constants defined under
another class. If you declare Enum is a member of a class, then by default it is static. You can access it with reference to enclosing class.
|
Code: |
package com.java2novice.enums;
public class MyEnumInsideClass {
private MyWrapper.Fruit myFruit;
public MyEnumInsideClass(MyWrapper.Fruit fruit){
this.myFruit = fruit;
}
public void getFruitDesc(){
switch (myFruit) {
case GRAPE:
System.out.println("A grape is a non-climacteric fruit.");
break;
case APPLE:
System.out.println("The apple is the pomaceous fruit.");
break;
case MANGO:
System.out.println("The mango is a fleshy stone fruit.");
break;
case LEMON:
System.out.println("Lemons are slow growing varieties of citrus.");
break;
default:
System.out.println("No desc available.");
break;
}
}
public static void main(String a[]){
MyEnumInsideClass grape = new MyEnumInsideClass(MyWrapper.Fruit.GRAPE);
grape.getFruitDesc();
MyEnumInsideClass apple = new MyEnumInsideClass(MyWrapper.Fruit.APPLE);
apple.getFruitDesc();
MyEnumInsideClass lemon = new MyEnumInsideClass(MyWrapper.Fruit.LEMON);
lemon.getFruitDesc();
MyEnumInsideClass guava = new MyEnumInsideClass(MyWrapper.Fruit.GUAVA);
guava.getFruitDesc();
}
}
class MyWrapper{
enum Fruit {
GRAPE, APPLE, MANGO, LEMON,GUAVA
}
}
|
|
Output: |
A grape is a non-climacteric fruit.
The apple is the pomaceous fruit.
Lemons are slow growing varieties of citrus.
No desc available.
|
|
|
|
|
List Of All Enum Programs:- Basic Enum example.
- How to call enum, which is defined inside a class?
- How to override toString() method with enum?
- How to create custom constructor enum?
|
|
|
Interface and its usage
Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant
declarations that must be defined by the class that implements it. Interfaces are useful for declaring methods that one or more classes
are expected to implement, capturing similarities between unrelated classes without forcing a class relationship and determining an object's
programming interface without revealing the actual body of the class.
Before you go and criticize the younger generation, just remember who raised them.
-- Unknown Author
|