Following is an example for the
java.lang.Enum.clone()
method.
DemoEnum.java
[java]package com.SPlessons;
enum Mobile1 {
Blackberry(2400), iPhone(3000);
int price;
Mobile1(int p) {
price = p;
}
int showPrice() {
return price;
}
}
public class DemoEnum {
public static void main(String args[]) {
System.out.println("Enums can never be cloned...");
DemoEnum t = new DemoEnum() {
protected final Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
};
System.out.println("List of CellPhones ");
for(Mobile1 m : Mobile1.values()) {
System.out.println(m + " costs " + m.showPrice() + " Dhirams");
}
}
}
[/java]
The functionality of this method is that enums will never be cloned and
CloneNotSupportedException()
will be raised.
[java]throw new CloneNotSupportedException();[/java]
Output: The result will be as follows.
[java]
Enums can never be cloned...
List of CellPhones
Blackberry costs 2400 Dhirams
iPhone costs 3000 Dhirams[/java]
By using java.lang.Enum.compareTo()
EnumDemo.java
[java]
package com.SPlessons;
import java.lang.*;
//enum showing topics covered under Tutorials
enum SPlessons {
Java, Hadoop, DataBase;
}
public class EnumDemo {
public static void main(String args[]) {
SPlessons t1;
SPlessons t2;
SPlessons t3;
t1 = SPlessons.Java;
t2 = SPlessons.Hadoop;
t3 = SPlessons.DataBase;
if(t1.compareTo(t2) > 0) {
System.out.println(t2 + " completed before " + t1);
}
if(t1.compareTo(t2) < 0) {
System.out.println(t1 + " completed before " + t2);
}
if(t1.compareTo(t3) == 0) {
System.out.println(t1 + " completed with " + t3);
}
}
}
[/java]
The functionality of
java.lang.Enum.compareTo()
method is to compare the enum with other objects, following is the syntax declaration of this method.
[java]public final int compareTo(E o)[/java]
Where
o
is nothing but object to be compared.
Output: The result will be as follows.
[java]Java completed before Hadoop[/java]
By using java.lang.Enum.ordinal() Method
EnumDemo.java
[java]
package com.SPlessons;
import java.lang.*;
//enum showing Mobile prices
enum Mobile {
Blackberry(4000), iPhone(1250), JIO(1325);
int price;
Mobile(int p) {
price = p;
}
int showPrice() {
return price;
}
}
public class EnumDemo {
public static void main(String args[]) {
System.out.println("CellPhone List:");
for(Mobile m : Mobile.values()) {
System.out.println(m + " costs " + m.showPrice() + " Dhirams");
}
Mobile ret = Mobile.Blackberry;
System.out.println("The ordinal is = " + ret.ordinal());
System.out.println("MobileName = " + ret.name());
}
}[/java]
The purpose of this java.lang.Enum.ordinal() method is to return the ordinals of an enumeration constant. Following is the declaration of this method.
[java]public final int ordinal()[/java]
Output: The result will be as follows.
[java]
iPhone costs 1250 Dhirams
JIO costs 1325 Dhirams
The ordinal is = 0
MobileName = Blackberry[/java]
By using java.lang.Enum.finalize() Method
EnumDemo.java
[java]
package com.SPlessons;
import java.lang.*;
//enum showing Mobile prices
enum Mobile {
Blackberry(1400), iPhone(1250);
int price;
Mobile(int p) {
price = p;
}
int showPrice() {
return price;
}
}
public class EnumDemo {
public static void main(String args[]) {
System.out.println("An enum class cannot have finalize methods...");
EnumDemo t = new EnumDemo() {
protected final void finalize() { }
};
System.out.println("CellPhone List:");
for(Mobile m : Mobile.values()) {
System.out.println(m + " costs " + m.showPrice() + " dhirams");
}
}
} [/java]
The functionality of this method is to return the classes of enum which does not have finalize methods. Following is the syntax of this method which was used in the code.
[java]protected final void finalize()[/java]
Output: The result will be as follows.
[java]
An enum class cannot have finalize methods...
CellPhone List:
Blackberry costs 1400 dhirams
iPhone costs 1250 dhirams
[/java]