Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instantiation again

 
Nazma Panjwani
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys........I was just curious as to how references worked....that's all..........I just used the Mammal class as an example.....
Some while back, I was working w/ arrays, and was trying to call a method that would change the size of an existing array...this method existed in a different class so I had to pass the array(whose size I wanted to change) as a parameter. Inside the method, I defined a temporary array (bigger array), copied all the values from the array that was passed, and then simply pointed the array (formal parameter) to the temp array. But ofcourse, the original array (actual parameter) did not point to the temp array (the bigger array). I figured out another way.....but then I became curious as to if there was a way in java which would allow this.....

By creating aliases, I can certainly change the state of an object, but I can't make one point to a new object (and have the other one automatically point to the new object as well).

Thanks for all your help.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem .

Just an FYI, if you need to do that again (expand the size of your array) you can use something like an ArrayList, which increases its size as needed with no work on your part.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well what you want to do is pretty much this ...



or if you want to be generic



however, Arrays.copyOf already does that for you ;-)
 
Nazma Panjwani
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,

Joe it was an assignment, so I had to come up with my own class.....and array........couldn't use ArrayList,
Sebastian, that is exactly how I did it after discovering my original method didn't work......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic