JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

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.
<< Previous Program | Next Program >>

List Of All Enum Programs:

  1. Basic Enum example.
  2. How to call enum, which is defined inside a class?
  3. How to override toString() method with enum?
  4. How to create custom constructor enum?
Knowledge Centre
wait Vs sleep methods
sleep(): It is a static method on Thread class. It makes the current thread into the "Not Runnable" state for specified amount of time. During this time, the thread keeps the lock (monitors) it has acquired.

wait(): It is a method on Object class. It makes the current thread into the "Not Runnable" state. Wait is called on a object, not a thread. Before calling wait() method, the object should be synchronized, means the object should be inside synchronized block. The call to wait() releases the acquired lock.
Famous Quotations
I don’t know the key to success, but the key to failure is trying to please everybody.
-- Bill Cosby

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.