i have set of constants. Enum will be the best option to define set of constants.even we can define all constants in a java class. what are the advantages and disadvantages over both? i mean which one is more advantageous? Please help me.
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would go for
enum. Enum gives you almost the same flexibility as classes (after all, enum is a class!), so you can declare fields, methods, constructors as you will. Forget about inheritance, but hey – you should favor composition over inheritance either way 😉Enum also gives you few cool features without any hustle:
switchandfor-loopstatementsvalueOf(String))I suggest having a look on ‘Effective Java by Josh Bloch‘, you will be amazed how powerful tools they really are.