Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Why use "new" operator with an instance variable of array type in accessor methods?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Why use "new" operator with an instance variable of array type in accessor methods?

    if you please explain more about array of objects and why we Do use new operator in accessor methods?
    accessor methods are only for returning values, but on this example they create object at line 25 ,instead of returning elements of array a, directly?

     
    public class ToyExample
    6 {
    7 private Date[] a;
    8 public ToyExample(int arraySize)
    9 {
    10 a = new Date[arraySize];
    11 for (int i = 0; i < arraySize; i++)
    12 a[i] = new Date();
    13 }
    14 public ToyExample(ToyExample object)
    15 {
    16 int lengthOfArrays = object.a.length;
    17 this.a = new Date[lengthOfArrays];
    18 for (int i = 0; i < lengthOfArrays; i++)
    19 this.a[i] = new Date(object.a[i]);
    20 }
    21 public Date[] getDateArray()
    22 {
    23 Date[] temp = new Date[a.length];
    24 for (int i = 0; i < a.length; i++)
    25 temp[i] = new Date(a[i]);
    26 return temp;
    27 }



    I'm sorry the title is long.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Why use "new" operator with an instance variable of array type in accessor methods?

    The getDate() method returns an array that it creates and fills using data from the a array.

    Read the API doc for the getDate() method to see what it is supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Why use "new" operator with an instance variable of array type in accessor methods?

    thanks
    but I'm asking why don't we displaying a element directly?
    I mean without create temp array?

    how i can read the API to read about?
    i use eclipse.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Why use "new" operator with an instance variable of array type in accessor methods?

    on this example they create object at line 25 ,instead of returning elements of array a, directly
    In fact it is on line 23 that they create the new object they will return (temp). The for loop is used to populate this new array with new Date objects.

    The reason is so that the caller does not get a reference to the private Date array a. If they did they could change the contents of the array - so that it would be all that private. It is common for accessors to return not some piece of private state, but a copy that the caller cannot modify.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    sciences (May 4th, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Why use "new" operator with an instance variable of array type in accessor methods?

    Quote Originally Posted by pbrockway2 View Post
    In fact it is on line 23 that they create the new object they will return (temp). The for loop is used to populate this new array with new Date objects.

    The reason is so that the caller does not get a reference to the private Date array a. If they did they could change the contents of the array - so that it would be all that private. It is common for accessors to return not some piece of private state, but a copy that the caller cannot modify.
    thank you
    i am appreciated for your help.

    Could you please write the statement of the caller?

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Why use "new" operator with an instance variable of array type in accessor methods?

    ToyExample eg = new ToyExample(666);
    Date[] arr = eg.getDateArray();
     
    arr[42] = new Date(); // this will *not* change the array entry in the ToyExample instance

  8. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Why use "new" operator with an instance variable of array type in accessor methods?

    thanks a lot.

    --- Update ---

    i want to display array a elements
    how to write method toString() for it?

  9. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Why use "new" operator with an instance variable of array type in accessor methods?

    Either use the Arrays.toString() method, or use a for loop to build up or display your string.

  10. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Why use "new" operator with an instance variable of array type in accessor methods?

    Quote Originally Posted by pbrockway2 View Post
    Either use the Arrays.toString() method, or use a for loop to build up or display your string.
    i've used for loop in toString() method.

Similar Threads

  1. Error message "Type result cannot be resolved or is not a field"
    By asreall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2012, 07:40 AM
  2. [SOLVED] Conditional Operator "?:" not compiling
    By mwardjava92 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2011, 05:40 PM
  3. Need something like a "Hashtable<String[], Integer>" kind of data type @@
    By Albretch Mueller in forum Java Theory & Questions
    Replies: 2
    Last Post: November 27th, 2010, 09:24 PM
  4. Is there an integer-type variable bigger than "long"?
    By bardd in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 02:43 PM
  5. Replies: 4
    Last Post: June 18th, 2009, 09:23 AM