• Post Reply Bookmark Topic Watch Topic
  • New Topic

About Memory allocation by new()  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Members,

I am glad to be a member of this forum.
I have a simple question:
If the jvm allocates memory for objects at runtime then how it is possible for compiler to allocate memory on calling new() for a constructor and then executing the constructor.

This mean both these memories are different?
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Memory allocation happens when a program is running; the compiler doesn't allocate memory for anything. You may have read in a book that when you write

new Foo()

the compiler generates the instructions to allocate memory for a Foo object, and then to call its constructor; that is correct. But all the compiler does is put these instructions into the class files; it isn't until the program is executed that these instructions are carried out. Thus the compiler decides when to allocate memory, but it's up to the JVM to actually do it.
 
vijayk kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for giving an indepth answer.
I have been looking for a source like this and Ranchers or really helping people like me.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!