Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.