How to make a constant pool defined in Java

Source: Internet
Author: User

First look at the example:

  1. /**
  2. * Method One
  3. */
  4. Interface Constantinterface {
  5. String SUNDAY = "SUNDAY";
  6. String MONDAY = "MONDAY";
  7. String Tuesday = "Tuesday";
  8. String Wednesday = "Wednesday";
  9. String Thursday = "Thursday";
  10. String FRIDAY = "FRIDAY";
  11. String SATURDAY = "SATURDAY";
  12. }
  13. /**
  14. * Method
  15. */
  16. Enum Constantenum {
  17. SUNDAY, MONDAY, Tuesday, Wednesday, Thursday, FRIDAY, SATURDAY
  18. }
  19. /**
  20. * Method Three
  21. */
  22. Class Constantclassfield {
  23. public static final String SUNDAY = "SUNDAY";
  24. public static final String MONDAY = "MONDAY";
  25. public Static final String Tuesday = "Tuesday";
  26. public Static final String Wednesday = "Wednesday";
  27. public Static final String Thursday = "Thursday";
  28. public static final String FRIDAY = "FRIDAY";
  29. public static final String SATURDAY = "SATURDAY";
  30. }
  31. /**
  32. * Method Four
  33. * http://www.ibm.com/developerworks/cn/java/l-java-interface/index.html
  34. */
  35. Class Constantclassfunction {
  36. private static final String SUNDAY = "SUNDAY";
  37. private static final String MONDAY = "MONDAY";
  38. private Static final String Tuesday = "Tuesday";
  39. private Static final String Wednesday = "Wednesday";
  40. private Static final String Thursday = "Thursday";
  41. private static final String FRIDAY = "FRIDAY";
  42. private static final String SATURDAY = "SATURDAY";
  43. public static String Getsunday () {
  44. return SUNDAY;
  45. }
  46. public static String Getmonday () {
  47. return MONDAY;
  48. }
  49. public static String Gettuesday () {
  50. return Tuesday;
  51. }
  52. public static String Getwednesday () {
  53. return Wednesday;
  54. }
  55. public static String Getthursday () {
  56. return Thursday;
  57. }
  58. public static String Getfirday () {
  59. return FRIDAY;
  60. }
  61. public static String Getsaturday () {
  62. return SATURDAY;
  63. }
  64. }
  65. Public class Testconstant {
  66. static final String day = "Saturday";
  67. public static void Main (string[] args) {
  68. System.out.println ("is today Saturday?");
  69. System.out.println (Day.equalsignorecase (constantinterface.saturday));
  70. System.out.println (Day.equalsignorecase (ConstantEnum.SATURDAY.name ()));
  71. System.out.println (Day.equalsignorecase (constantclassfield.saturday));
  72. System.out.println (Day.equalsignorecase (constantclassfunction
  73. . Getsaturday ()));
  74. }
  75. }

Method one uses an interface (Interface) in which the default is the static final attribute.

Method two uses the enum type introduced in Java 5.0.

Method three takes the method of using static final modifier variables in a normal class.

Method Four is similar to method three, but the function is used to get the constant.

The first definition of global variables seems to violate Java's object-oriented encapsulation characteristics, increasing coupling. So the best way is to avoid defining global variables. If it is a parameter, etc., you can write to the configuration file. If it is really necessary, method two is the most recommended. Method Three is everyone can think of, very intuitive. Method One and method three are essentially the same. Method Four provides flexibility, specifically referencing "1".

How to make a constant pool defined in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.