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 trial

Java Java Objects (Retired) Meet Objects Creating Classes

Qian Wu
Qian Wu
1,169 Points

When you initialize an new obj of pezDispenser by using "new". Does it exist on the stack or heap?

same as question

1 Answer

Joe Brown
Joe Brown
21,465 Points

New objects are allocated on the heap. As I understand it, the stack is for function calls and contains the parameters and local variables for that function. When you create a new object inside of a method the reference variable you store it in is stored on the stack with the method and local variables, but the actual object itself is still stored in the heap, you just have your reference to it on the stack in that case.