0% found this document useful (0 votes)
17 views

Use Values : "Here Are All Week Constants"

The values() method returns an array containing all the enumeration constants. The valueOf() method returns the enumeration constant whose value matches the passed string. valueOf() throws an IllegalArgumentException if the enum type does not match the defined name, or a NullPointerException if the enumType or name is null.

Uploaded by

Keith Veigas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Use Values : "Here Are All Week Constants"

The values() method returns an array containing all the enumeration constants. The valueOf() method returns the enumeration constant whose value matches the passed string. valueOf() throws an IllegalArgumentException if the enum type does not match the defined name, or a NullPointerException if the enumType or name is null.

Uploaded by

Keith Veigas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

All enumerations automatically contain two predefined methods: values( ) and

valueOf( ).

1. public static enum-type[ ] values( )


2. public static enum-type valueOf(String str)

The values( ) method returns an array that contains a list of the enumeration
constants.
The valueOf( ) method returns the enumeration constant whose value
corresponds to the string passed in str.

The valueOf() method throws:

1. IllegalArgumentException, if the defined enum type is inconstant with


defined name or an enum type is not illustrated by the defined class
object.
2. NullPointerException, if enumType or name represents null value.

enum Week {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturaday, Sunday
}

public class MainClass {


public static void main(String args[]) {
System.out.println("Here are all Week constants");

// use values()
Week allWeek[] = Week.values();
for (Week aday : allWeek) {
System.out.println(aday);
}

}
}
Here are all Week constants
Monday
Tuesday
Wednesday
Thursday
Friday
Saturaday
Sunday

enum Parts{

Skin, Muscles,Bones,Organs,Tissue;

public class Enum_valueOfMethodExample1 {

public static void main(String[] args) {

System.out.println("The part which is exposed to the environment is :");

for(Parts part : Parts.values()){

int i = part.ordinal()+1;

System.out.println(i+" "+part);

Parts part = Parts.valueOf("Skin");

System.out.println("\nAns: "+part);

}
}
Output:
The part which is exposed to the environment is :
1 Skin
2 Muscles
3 Bones
4 Organs
5 Tissue

Ans: Skin

You might also like