Dear Bill, I have a question ! All we know is that, objects including instance variables are created in heap and all methods and local variables, and reference variables are created on the stack. Suppose I have, class Animal { int size; public void methodA(){ // a line of code } } now If I create an Animal object and I refer to it to a reference variable called dog i.e. Animal dog = new Animal(); Then I invoke the methodA() in my main() method like this dog.methodA(). Now My Question Is: How's going to object use that method cause object is in Heap and Method is in Stack!? And as far as I know from your video there's only a pointer from stack which is reference variable pointing at object which is in Heap.
The reference variable dog would be on the stack, but dog would be pointing to an object of type Animal on the heap. When you called dog.methodA(), it would call the method from the object on the heap that the dog variable was pointing to. Bear in mind this analogy is not a perfect representation of what Java does in the background.
Let’s say we create an object Cat anonymous = new Cat(); what does anything have to do with the Cat in the beginning it’s the type of the object yeah but like what does that mean. So anonymous is the memory reference so is the anonymous pointing to Cat or something??
Think of it like this: Imagine that you created a name for a real life cat. In this case, the name is "anonymous." Just because you have a cat name, doesn't mean you have an actual cat. If you say "hey anonymous, go clean your fur," nothing will happen because there is no actual cat attached to that name. If you want a cat to actually do anything, you need to create a physical cat and then have the name anonymous point at the actual cat. Now that you have the anonymous name pointing at a physical cat, you can say things like "Hey anonymous, go catch a mouse."
fall() is a static method which means it belongs to the class. Since the method resides in the class, an instance of the class (object) doesn't need to exist to access it.
📌 Subscribe For The Latest Videos: bit.ly/36H70sy 📌
.
💻 All Java Tutorials: bit.ly/JavaTutorialsRUclips 💻
.
🤖 Learn Java In 3 Hours: bit.ly/JavaIn3Hours 🤖
This is the best explanation I have found on RUclips! Thank you!
Nice explanation thank you
I hope I get a 5 of AP CS A 🙏
Dear Bill, I have a question ! All we know is that, objects including instance variables are created in heap and all methods and local variables, and reference variables are created on the stack. Suppose I have,
class Animal {
int size;
public void methodA(){
// a line of code
}
}
now If I create an Animal object and I refer to it to a reference variable called dog i.e. Animal dog = new Animal(); Then I invoke the methodA() in my main() method like this dog.methodA(). Now My Question Is: How's going to object use that method cause object is in Heap and Method is in Stack!? And as far as I know from your video there's only a pointer from stack which is reference variable pointing at object which is in Heap.
The reference variable dog would be on the stack, but dog would be pointing to an object of type Animal on the heap.
When you called dog.methodA(), it would call the method from the object on the heap that the dog variable was pointing to.
Bear in mind this analogy is not a perfect representation of what Java does in the background.
perfect explanation ,Thank u so much!
Let’s say we create an object Cat anonymous = new Cat(); what does anything have to do with the Cat in the beginning it’s the type of the object yeah but like what does that mean. So anonymous is the memory reference so is the anonymous pointing to Cat or something??
Think of it like this:
Imagine that you created a name for a real life cat. In this case, the name is "anonymous." Just because you have a cat name, doesn't mean you have an actual cat. If you say "hey anonymous, go clean your fur," nothing will happen because there is no actual cat attached to that name.
If you want a cat to actually do anything, you need to create a physical cat and then have the name anonymous point at the actual cat.
Now that you have the anonymous name pointing at a physical cat, you can say things like "Hey anonymous, go catch a mouse."
Excellent explanation
Thanks for the feedback!
Great!
Watch at 1.25 speed, thank me later
Good advice.
link for memory handling ruclips.net/video/LTnp79Ke8FI/видео.html
What is happening on the stack and heap when we call catzilla.fall() and clawdia.fall()?
fall() is a static method which means it belongs to the class. Since the method resides in the class, an instance of the class (object) doesn't need to exist to access it.