0

I am new student java learning its basics

My objective is to create a Enum which contains various categories like Emails, Username, Passwords, MaterialType etc. Further I wanted that within one Category I can declare various strings and my sample code is as below:

public enum MyEnums {
    Usernames
    {
        public String toString()
        {
            return "This is a GmailUsername";
        }
        /*public String toString()
        {
            return "This is a GalleryComment";
        }*/
    },
    Password
    {
        public String toString()
        {
            return "This is a public password";
        }
    /*  public String GmailPassword()
        {
            return "This is a Gmail Password";
        } */

    },
    Emails
    {
        public String toString()
        {
            return "This is a public contact email address";
        }
        /* public String EmailAccount()
        {
            return "This is a public Email Account address";
        } */

    },
    PhoneNumbers
    {
        public String toString()
        {
        return "This is Phonenumber";
        }
        /*  public String Phone()
        {
            return "This is a phone number";
        }*/
        }
    }

and I call the code as

public static void main (String args[])
   {

       System.out.println(MyEnums.Emails);
       System.out.println(MyEnums.Usernames );
       System.out.println(MyEnums.PhoneNumbers);
       System.out.println(MyEnums.Password);

   }

My question is why on using second string type function it is giving error, example In the password category for GmailPassword() why it is not working.

Is there is any other way to declare multiple strings in enum in category wise manner like

public Enum myEnum{
Category1
{
"String 1","String2",......."String N"
}
.......
.......
.......
.......
CategoryN
{
"String 1","String2",......."String N"
}
3
  • I think you should create a common interface for the strings and implement that for each enum value. Commented Sep 12, 2014 at 11:38
  • You are creating private anonymous inner classes for each enum constant. Only methods declared in the base class MyEnum can be accessed from the outside. Commented Sep 12, 2014 at 11:52
  • @Holger Sir please validate public Enum MyEnum { public String Category1(String s) { switch(s) case 'Delete' : “This is a string 1”; case 'Update': “This is a update” case “Error”: “You have validation Error” } } and can call by String s='delete' System.out.println(MyEnum.Category1(s)); Commented Sep 12, 2014 at 12:03

3 Answers 3

3

maybe this helps?

 public enum MyEnum {
    Emails("mail1", "mail2", "mail3"), 
    Usernames("username1", "username2"), 
    CategoryN("a", "b", "c");

    private String[] strings;

    private MyEnum(String... strings) {
        this.strings = strings;
    }

    @Override
    public String toString() {
        return Arrays.toString(strings);
    }

    public String getString(int index) {
        return strings[index];
    }

}

Main

    public static void main(String[] args) {
        System.out.println(MyEnum.Emails);  //[mail1, mail2, mail3]

        System.out.println(MyEnum.Emails.getString(1)); //mail2
    }
Sign up to request clarification or add additional context in comments.

6 Comments

+1 good approach. But shouldn't the string property be public final, or provide an accessor ?
@Pierre Henry: no it's not required but might be helpful. But you still need to remember that one could modify content of strings array.
How to call it in the main function I am calling it by using System.out.println(MyEnum.Emails); System.out.println(MyEnum.Usernames); but not getting the single string instead I am getting whole enum.
Sir it helps and returning the data the way I need but don't know how to call it in main method. Please give me hints it will be your greatness.
Thank you sir :) I got What I needed the most love you sir @Criban
|
1

You can create an enum with Objects instead of just Strings. That way you can access all the properties of those objects in a clean manner.

1 Comment

Sir I am new to coding so it will be your greatness if you will programmatic illustration thanks
1

It seems to me that instead of an Enum with categories, you should have an interface that you make each category enum implement. That way you have N enums that each implement the category interface, and inside each of those you have the strings as the enum constants.

1 Comment

please please give me an coding illustration of your approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.