Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,888 questions

51,815 answers

573 users

How to use toString() method to get string representation of the enum value in Java

1 Answer

0 votes
public class MyClass {
    public enum Color {
        RED("#FF0000"),
        GREEN("#00FF00"),
        BLUE("#0000FF");
    
        private String code;
    
        private Color(String code) {
            this.code = code;
        }
    
        @Override
        public String toString() {
            return code;
        }
    }
    public static void main(String args[]) {
        String red = Color.RED.toString();
        String green = Color.GREEN.toString();
        String blue = Color.BLUE.toString();

        System.out.println(red);
        System.out.println(green);
        System.out.println(blue);
    }
}



/*
run:

#FF0000
#00FF00
#0000FF

*/

 



answered Nov 15, 2023 by avibootz

Related questions

1 answer 220 views
1 answer 156 views
2 answers 203 views
203 views asked Apr 25, 2017 by avibootz
2 answers 158 views
1 answer 148 views
1 answer 152 views
152 views asked Nov 9, 2021 by avibootz
...