• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Stack/Heap -Methods in the class?

Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

1.The methods defined in the class ,do they go on stack or heap?
I read somewhere ,Object with its instance-variables and methods is stored on heap .
2. Also i have another doubt

example code is :

class Dog{
public static void main(String args[])
Dog d=new Dog(); //Object reference to Dog



Does his mean there are 2 references to Dog -objects in memory.one is ' this' instance run by JVM ,another user-programmed d.
Java Cowboy
Posts: 16084
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Methods are executable code, which is stored in a separate memory area from data (variables). Note that there is not a copy of the code of the methods of a class for each instance of that class - that would be a waste of memory, because the code is the same for all instances of a class. Executable code is certainly not stored on the stack.

2. You have one variable 'd' which refers to one Dog object. 'this' refers to the current object. If you'd call a non-statice method on d, then inside that method you could use 'this', which would refer to the same Dog object as d. There are ofcourse not 2 Dog objects.
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic