How to Call Enum Inside a Function in Java


In this example we will show how to call enumeration type within a main function.

Source Code

package com.beginner.examples;

public class EnumInsideClass {

	public static void main(String[] args) {
		
		//Use defined enumerations
		Gender gender = EnumInsideClass.Gender.MADAM;
		
		Gender gender2 = EnumInsideClass.Gender.MALE;
		
		System.out.println(gender.toString());
		
		System.out.println(gender2.toString());

	}
	
	//Define an enumeration in this class
	enum Gender{
		
		MALE("This sex is male"),MADAM("This sex is female");
		
		private String name;
		
		private Gender(String name)
		{
			this.name = name;
		}
		
		public String toString()
		{
			return this.name;
		}
		
	}

}

Output:

This sex is female
This sex is male
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments