r/javahelp • • 8d ago

Java Beginner – Struggling with OOP and Understanding Stack and Heap

Hi everyone!
I’ve been learning Java for a few weeks now, and I’m starting to get into OOP. However, I’m finding it difficult to fully understand some of the core concepts, especially how objects work and the difference between the stack and heap memory.
I don’t want to just memorize definitions. I want to actually understand what’s happening behind the scenes and how everything connects.
For those of you who have been through the same learning process:
What helped you understand OOP?
How did you learn the difference between stack and heap?
Are there any resources, exercises, or learning methods you would recommend?
I’d really appreciate any advice from more experienced Java developers.

14 Upvotes

15 comments sorted by

View all comments

2

u/Far-Bullfrog161 6d ago

What made it click for me was drawing it out on paper. When a method runs it gets its own frame on the stack with its local variables, and when it returns that frame is gone. When you write new Dog(), the object itself lives on the heap and the variable on the stack just holds a reference to it, kind of like an address. So Dog a = new Dog(); Dog b = a; gives you two references and one object, and changing b.name changes what a sees too. Try a tiny program like that, then add a method that takes a Dog and changes its name, and another that reassigns the parameter to a new Dog, and guess what prints before you run it. That second case trips up a lot of people and it's where Java's pass-by-value with references shows up. Once you can picture objects sitting on the heap with their own state and methods reading or changing that state, OOP gets a lot less abstract.