• 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 ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

static vs instance methods

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

for each copy of object we create, we get new instance variables...
i.e for n objects ...n no of instance variables are stored in heap.
methods are stored in stack

but where is stack memory located ?
is it part of object heap memory?

static variables/methods are 1 per class
what bout instance methods?
how many instance methods are created for n objects?
and also where is static methods stored?

can any one clarify me...
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables are one per class
Instance variables are one per instance (each object has its own copy)
Static methods/variables are on the stack, while objects and all their data (including their instance variables) are on the heap.
From what I understand, the stack is a set amount of memory given to a particular program. So when you start the program, the stack is a preset size, depending on the program, and it does not grow or shrink. The heap on the other hand is any free memory on your system. When you call the 'new' operator, the program requests some amount of memory from the system's free RAM. This whole concept is based on your entire system, but in the case of Java, the Virtual Machine is the system.
As far as instance methods, I don't know if they are copied for each object, or if there is one copy somewhere on the heap. I'm sure someone else knows. If I had to guess, I'd say it's copied per instance in Java. But that's purely an uneducated guess.
 
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umamahesh Javvadi, welcome to the Ranch.

There is more detail about how the memory is allocated in the JVM in this page from the Jakarta BCEL project. Much more detail, which requires very careful reading!
If I remember correctly, it tells us there is one copy of each method for each class. Also there is one copy of each static variable for each class, which can be shared between all the instances. The whole lot all together is called a "class object."

There are as many instances in the "heap" as you can create and fit in. Each instance on the heap has details of its fields, but does not contain its methods. Rather, it has a reference back to its own class object, where the methods are. Whenever you call a method on an object, the details are passed back to the class object, along with a reference to the instance on the heap.

They explain it a lot better in BCEL!
 
Campbell Ritchie
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I have given the impression that static variables are in the same part of memory as methods. Not what I intended to mean. Not sure whether it is true or false.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ritchie, the article suggested by you is very informative.
And I still wonder why haven't you been promoted to bartender after so much participation. Hope this goes to the senior Java Ranchers..


Sid
 
umamahesh javvadi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,

thanks for your valuable information on stack ....

but i still have small doubt...
can any one explain clearly what this means....
"The methods of an object defines its behavior and are called instance methods.It is important to note that these methods pertain to each object of class.This should not be confused with the implementation of the methods, which is shared by all instances of the class."

Reference:A Programmer's guide to Java Certification-Khalid A.Mughal
1st chapter,section 1.4,page 46....
 
Campbell Ritchie
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only too pleased to help . . .

They know what an awkward so-and-so I would be; if I were bartender nobody would ever get a drink .

About that quote from Mughal: I can make various interpretations of it.
  • Get a different book which you can actually understand.
  • Go onto the part of the Ranch called Bunkhouse Books and write a review of that book. What is the use of a book if you can't understand it?
  • Maybe . . . Therre is one copy of each method which is shared by every instance, but each instance produces different results and effects because its fields have different contents and values.
  • Maybe . . .
     
    umamahesh javvadi
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi,

    thanks Campbell for your clarification

    i am following head first and Java Tutorial(from sun).
    i am planning for Java Certification.
    First may be it will take some time for me getting the fundamentals clear.
    i am new to this forum.hope i can get some good assistance from you all.


    UmaMahesh Javvadi
     
    What does a metric clock look like? I bet it is nothing like this tiny ad:
    New web page for Paul's Rocket Mass Heaters movies
    https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
    reply
      Bookmark Topic Watch Topic
    • New Topic