|
|
Program: Basic Enum example.
Description: |
This example defines a basic enum type called Fruits. It contains few constants representing fruits.
The switch case uses these constants to execute conditions.
|
Code: |
package com.java2novice.enums;
public class MyBasicEnum {
private Fruits myFruit;
public MyBasicEnum(Fruits 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[]){
MyBasicEnum grape = new MyBasicEnum(Fruits.GRAPE);
grape.getFruitDesc();
MyBasicEnum apple = new MyBasicEnum(Fruits.APPLE);
apple.getFruitDesc();
MyBasicEnum lemon = new MyBasicEnum(Fruits.LEMON);
lemon.getFruitDesc();
MyBasicEnum guava = new MyBasicEnum(Fruits.GUAVA);
guava.getFruitDesc();
}
}
enum Fruits {
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?
|
|
|
Can we call servlet destory() from service()?
As you know, destory() is part of servlet life cycle methods, it is used to kill the
servlet instance. Servlet Engine is used to call destory(). In case, if you call destory
method from service(), it just execute the code written in the destory(), but it wont
kill the servlet instance. destroy() will be called before killing the servlet instance
by servlet engine.
Success consists of going from failure to failure without loss of enthusiasm.
-- Winston Churchill
|