Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJoannie Huang
6,195 PointsAbout "mPages[0] = new Page();"
Hi Ben, I want to ask about why in the video, you wrote a line:
mPages[0] = new Page();
Isn't the mPages array have already been created an Page object by the following code?
mPages = new Page[7];
Could you explain a little bit further regarding the difference if there is without creating an object on each item in array? Thanks :)
2 Answers
Jack Middlebrook
19,746 PointsIn the example, the line:
mPages = new Page[7];
creates the array which can hold references to 7 Page objects. It doesn't actually create 7 Page objects but allocates enough memory for 7 Page objects. The Page objects are then created separately with the following:
mPages[0] = new Page();
Now the first element in the array points to a new Page object that has been created.
John MacDonald
8,593 Pointsjust jumping in here to say thanks as well. this cleared up my same confusion
Joannie Huang
6,195 PointsJoannie Huang
6,195 PointsThanks, Jack! I think I understand what you meant :)
It's clearly enough.
ian sobo
3,851 Pointsian sobo
3,851 Pointsjust jumping in here to say thanks as well. this cleared up my same confusion
Nitin George Cherian
4,410 PointsNitin George Cherian
4,410 PointsHello Jack,
Thanks for the answer.
For the code,
mPages = new Page[7];
You have given the comment " It doesn't actually create 7 Page objects but allocates enough memory for 7 Page objects." It is true that it does not actually create 7 Page objects. However, it allocates enough memory for 7 Page object references instead of 7 Page objects. Here is answer from stackoverflow.com which explains the same. http://stackoverflow.com/a/10044424/1220250
Please correct me if I am wrong.