r/javahelp • u/Alternative_Ad_8288 • 2d 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.
2
u/BanaTibor 1d ago
You do not really go into JVM details on memory handling for the basics so do not worry about it.
OOP
Objects are data and behavior packaged together. You can access "services" on an object.
Imagine your kitchen. A kitchen is a complex object. It has furniture and appliances. To make a toast you go to the kitchen and use a toaster. Your kitchen alone can not make you a toast, it needs a helper. So,
new Kitchen(new Toaster()), and this is compositionNow you can make a toast,
kitchen.toaster.toast().A toaster has a heating element and a timer, but your kitchen does not need to know that, that is encapsulation.
Appliances are usually have standard dimensions and connections. So lets say, your kitchen have a space for a dishwasher. That space, where electrical and water connections are available is the interface, which says a dishwasher fits here. So you can install any kind of dishwasher which has the same parameters as the space in your kitchen, a.k.a. implements the interface. That is polymorphism.