Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Fubarable

Thread: [Asking] TestEnum2.java

  1. #1
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default [Asking] TestEnum2.java

    Dear all, I'm a newbie in java programming.
    Yesterday, I read and rewrite codes from a java book. The codes are below:

    //================================================== ===
    Java Code:
    enum days {
       SUNDAY(true) {
          public int workTime() {
             return 0;
          }
       },
       MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY {
          public int workTime() {
             return 5;
          }
       };
       private boolean holiDay;
    
       days() {
          this.holiDay = false;
       }
    
       days(boolean holiDay) {
          this.holiDay = holiDay;
       }
    
       public boolean isHoliDay() {
          return holiDay;
       }
    
       public int workTime() {
          return 8;
       }
    }
    
    public class TestEnum2 {
       public static void main(String[] args) {
          days daY = days.MONDAY;
          System.out.println("Holiday      : " + daY.isHoliDay());
          System.out.println("Work Hours : " + daY.workTime());
    
          daY = days.SATURDAY;
          System.out.println("Holiday      : " + daY.isHoliDay());
          System.out.println("Work Hours : " + daY.workTime());
    
          daY = days.SUNDAY;
          System.out.println("Holiday      : " + daY.isHoliDay());
          System.out.println("Work Hours : " + daY.workTime());
       }
    }
    //================================================== ===

    And the result is:
    //================================================== ===
    Java Code:
    Holiday        : false
    Work Hours     : 8
    Holiday        : false
    Work Hours     : 5
    Holiday        : true
    Work Hours     : 0
    //================================================== ====

    My question :
    1. In this line
    Java Code:
    MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY{
    			public int workTime(){
    				return 5;
    			}
    		};
    I think the value return to 5, but why value of SATURDAY(false) is different with MONDAY(false) ?

    2. How could this line
    Java Code:
    public int workTime(){
    			return 8;
    		}
    effect the value of SATURDAY(false) ?

    Would you like to explain me about this, please ?
    I'm newbie and I need to learn a lot.
    Thanks. :)
    Attached Thumbnails Attached Thumbnails [Asking] TestEnum2.java-testenum2.jpg  
    Last edited by Fubarable; 10-21-2011 at 09:58 PM. Reason: QUOTE TAGS CHANGED TO CODE TAGS

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: [Asking] TestEnum2.java

    Quote Originally Posted by expert_developer View Post
    My question :
    1. In this line
    Java Code:
       MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY {
          public int workTime() {
             return 5;
          }
       };
    I think the value return to 5, but why value of SATURDAY(false) is different with MONDAY(false) ?
    Both SATURDAY and MONDAY use the default constructor and thus set the holiDay field to false, but SATURDAY has an override for the workTime() method (returning 5), while MONDAY and all the other weekday enums use the default workTime() method which returns 8.


    2. How could this line
    Java Code:
    public int workTime(){
       return 8;
    }
    effect the value of SATURDAY(false) ?
    It doesn't because SATURDAY overrides this method.

  3. #3
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: [Asking] TestEnum2.java

    Quote Originally Posted by Fubarable View Post
    Both SATURDAY and MONDAY use the default constructor and thus set the holiDay field to false, but SATURDAY has an override for the workTime() method (returning 5), while MONDAY and all the other weekday enums use the default workTime() method which returns 8.




    It doesn't because SATURDAY overrides this method.
    Which part of codes that show override ?
    :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: [Asking] TestEnum2.java

    This is MONDAY's declaration:
    Java Code:
    MONDAY
    This is TUESDAY's declaration:
    Java Code:
    TUESDAY
    This is SATURDAY's declaration:
    Java Code:
    SATURDAY {
       // method override below
       public int workTime() {
          return 5;
       }
    };
    See the difference?

  5. #5
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: [Asking] TestEnum2.java

    So, the value of MONDAY up to FRIDAY automatically return to 8 ?
    And only SATURDAY has value return to 5 ?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: [Asking] TestEnum2.java

    Yes. Monday -> Friday use the workTime() method at the end of the enum. But Saturday uses the overridden form that returns 5.

  7. #7
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: [Asking] TestEnum2.java

    I see.
    I thought that MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY have same value return with
    SATURDAY {
    public int workTime() {
    return 5;
    }
    };

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: [Asking] TestEnum2.java

    It can be confusing; so you're not alone. Perhaps giving each Days enum its own line would make it less confusing:

    Java Code:
    enum Days {
       SUNDAY(true) {
          public int workTime() {
             return 0;
          }
       },
       MONDAY, 
       TUESDAY, 
       WEDNESDAY, 
       THURSDAY, 
       FRIDAY, 
       SATURDAY {
          public int workTime() {
             return 5;
          }
       };
       private boolean holiDay;
    
       Days() {
          this.holiDay = false;
       }
    
       Days(boolean holiDay) {
          this.holiDay = holiDay;
       }
    
       public boolean isHoliDay() {
          return holiDay;
       }
    
       public int workTime() {
          return 8;
       }
    }
    Oh and "days" should be capitalized, "Days" since it represents the name of the enums.
    expert_developer likes this.

  9. #9
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: [Asking] TestEnum2.java

    Oh I see.
    Now I understand about it. Thanks for your explanation and suggestion.
    I hope you'ld like to teach me more about java programming because I need to learn a lot.
    :)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •