Results 1 to 4 of 4
  1. #1
    sherifhanna82 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Calendar Class and Accessor Methods

    Greetings all,

    I am a new member of the Java fan club and by new I mean about 2 months into learning Java.

    I am comfortable with the language basic fundamentals, statements, variables, objects, and classes. I am still a complete stranger to GUI, applets and heavy programming.

    I do have two questions:
    1. What is the purpose of accessor methods if they ultimately still allow the manipulation of the object's fields? To me, Employee.name = "John smith" is the same as Employee.setName("John Smith"). Why the extra lines of accessor methods code in the Employee class?
    2. Why is it that when creating a calendar instance (Calendar TodayDate = Calendar.getInstance();) I don't need to use the word "new" to create the TodayDate object, as I would with any other instance of a class? I thought that's because Calendar is a JAVA API class, but that's not the case for instance with SimpleDateFormat.

    Again, I am new to JAVA and my only OOP experience is intermediate Excel VBA, where objects and classes were created already for the most part.

    Thanks for your help,
    S.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,950
    Rep Power
    4

    Default Re: Calendar Class and Accessor Methods

    The answer to your first question it is that is a preferred practice is to use getter and setter methods to access fields.
    • By providing a consistent access method it allows the internal storage of those fields to be changed without causing problems with applications that use the class.
    • It also allows the implementer to enforce invariants on the fields (e.g. ensure > 0 or within a range).


    The reason that you must use the getInstance method for calendar is because the actual implementation is hidden. Calendar itself is an abstract class so it can't be instantiated directly. Usually, instance methods are used to enforce the use of a single instance. In this case, I can't really explain why the designers chose to use this design pattern as opposed to allow direct instantiation.

    I tend not to care as long as I can get a working reference to an implementation of the class.

    Regards,
    Jim
    Last edited by jim829; 04-19-2013 at 05:01 PM. Reason: YAT
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: Calendar Class and Accessor Methods

    Quote Originally Posted by sherifhanna82 View Post
    1. What is the purpose of accessor methods if they ultimately still allow the manipulation of the object's fields? To me, Employee.name = "John smith" is the same as Employee.setName("John Smith"). Why the extra lines of accessor methods code in the Employee class?
    2. Why is it that when creating a calendar instance (Calendar TodayDate = Calendar.getInstance();) I don't need to use the word "new" to create the TodayDate object, as I would with any other instance of a class? I thought that's because Calendar is a JAVA API class, but that's not the case for instance with SimpleDateFormat.
    1) Those methods hide implementation, e.g. the accessors may do some checks before they set the private variable.
    2) The getInstance() method does the 'new ...' for you, i.e. it creates the instance for you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    sherifhanna82 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Calendar Class and Accessor Methods

    Thank y'all for the prompt and very thorough replies. I am looking forward to mastering the science of Java :)

Similar Threads

  1. Accessor and mutator methods help
    By ajb5876 in forum New To Java
    Replies: 11
    Last Post: 02-15-2013, 03:59 PM
  2. plz help me with this class, calendar
    By hamed in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 10:58 PM
  3. Accessor & Mutator methods understanding..
    By 6thDAY in forum New To Java
    Replies: 3
    Last Post: 08-14-2010, 07:27 PM
  4. Replies: 6
    Last Post: 12-02-2008, 11:15 PM
  5. How to use Calendar class
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-04-2008, 02:33 PM

Posting Permissions

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