JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

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.
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?
Java2Novice - YouTube Channel
  1. Python Tutorial for Beginners
Knowledge Centre
What is adapter class?
An adapter class provides the default implementation of all methods in an event listener interface. Adapter classes are very useful when you want to process only few of the events that are handled by a particular event listener interface. You can define a new class by extending one of the adapter classes and implement only those events relevant to you.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

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 © 2019 by Nataraja Gootooru. All Rights Reserved.