aspose file tools*
The moose likes Beginning Java and the fly likes static vs instance methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static vs instance methods" Watch "static vs instance methods" New topic
Author

static vs instance methods

umamahesh javvadi
Greenhorn

Joined: Apr 22, 2007
Posts: 7
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...
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
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.


Studying for SCJP 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
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.
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
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

Joined: Apr 22, 2007
Posts: 7
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
Sheriff

Joined: Oct 13, 2005
Posts: 38865
    
  23
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

    Joined: Apr 22, 2007
    Posts: 7
    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
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: static vs instance methods