Re: Objects in java
Roedy Green wrote:
On Sat, 4 Jul 2009 03:38:11 -0700 (PDT), Prasoon
<prasoonthegreat@gmail.com> wrote, quoted or indirectly quoted someone
who said :
The memory is allocated for 'x' at compile time by the compiler or at
the run time by the jvm????
for a local variable the slot on the stack in allocated by the
compiler, but he space itself is allocated at run time.
For a field, the index is assigned by the compiler, but the memory
itself is allocated at run time.
To understand this more deeply you need to understand a bit about how
byte code works.
see http://mindprod.com/jgloss/jasm.html
http://mindprod.com/jgloss/jvm.html
It seems to me that the O.P. has asked a nebulous question
because he has not yet understood Java's execution model. The
missing link (I think) is that there's no "link" in Java, no
pre-execution phase that gathers together all the components
of a program, resolves references, assigns addresses, and so on.
Java's execution is entirely dynamic (well, after a little
indispensable kernel of bootstrap, perhaps): The JVM is told the
name of a class it is to run, loads that class, finds its main()
method, and calls it. That main() method in turn refers to other
classes, and the JVM loads them as needed -- and only *if* needed.
It's all demand-driven; only the essential classes that the JVM
itself requires to come to life are preordained.
... which means that essentially *all* memory is allocated
at run-time. Static fields are allocated when the JVM finds it
necessary to load their classes, and deallocated when and if the
JVM decides to allow the garbage collector to reclaim the classes.
Instance variables are allocated when the program does a `new',
and deallocated when and if the GC reclaims their objects. Method
variables (warning: loose language) are allocated when the method
is called (loose language) and deallocated when the method
terminates. *Everything* is a run-time allocation of one kind
or another.
(Methinks the O.P. has some other misconceptions about Java,
too, as witness his use of an `int' to exemplify an object.)
--
Eric Sosman
esosman@ieee-dot-org.invalid