Home » Java programming language

Java EnumSet clone() Method with Example

EnumSet Class clone() method: Here, we are going to learn about the clone() method of EnumSet Class with its syntax and example.
Submitted by Preeti Jain, on February 13, 2020

EnumSet Class clone() method

  • clone() method is available in java.util package.
  • clone() method is used to return a shallow copy of this EnumSet.
  • clone() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • clone() method does not throw an exception at the time of cloning EnumSet.

Syntax:

    public EnumSet clone();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is EnumSet, it returns cloned copy of this EnumSet.

Example:

// Java program is to demonstrate the example of
// clone() method of EnumSet

import java.util.*;

public class CloneOfEnumSet {
    // Initialize a enum variable
    // with some constants

    public enum Colors {
        RED,
        BLUE,
        GREEN,
        PURPLE,
        YELLOW
    };

    public static void main(String[] args) {
        // Here , we are creating two EnumSet
        // First EnumSet is intiatize with some 
        // values and Second EnumSet is empty
        EnumSet es = EnumSet.allOf(Colors.class);
        EnumSet < Colors > clone_es = null;

        // Display EnumSet
        System.out.println("EnumSet (es): " + es);
        System.out.println("EnumSet (clone_es): " + clone_es);

        // By using clone() method is to
        // clone EnumSet es 
        clone_es = es.clone();

        // Display Cloned EnumSet
        System.out.println("es.clone(): " + clone_es);
    }
}

Output

EnumSet (es): [RED, BLUE, GREEN, PURPLE, YELLOW]
EnumSet (clone_es): null
es.clone(): [RED, BLUE, GREEN, PURPLE, YELLOW]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.