This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it possible to create a object on stack

 
siddhu Math
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it possible for me to create an object on stack as i know that objects are created on heap.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it isn't. All method-local primitive types and and references are put on the stack, all objects are put in the heap. No ifs and buts about it.

One reason I can think why they did is that it removes one commonly made error: you pass the stack-based object to a method that stores a reference to that object. Then the object goes out of scope, is removed from the stack, and the reference points to something undefined. Next when you want to access the "object" through the reference, you're in a world of hurt since the object is no longer there - and nobody knows what is.
 
siddhu Math
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok if objects are created on heap then why not those on stack since you know static objects are created on stack.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since you know static objects are created on stack.


Where in the world did you get that idea? What do you think a "static object" is?



(Why do I ask? As an occasional author I want to find out where bad information comes from.)

Bill
 
siddhu Math
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it i was really in a wrong concept
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hate to complicate matters, but believe it or not, Java objects can exist on the stack !! Java 6 added "escape analysis" as an optimization tool.

If an object (referenced by a local variable) can be determined to not escape the method (which is really not that easy to determine), the object will be created on the stack. It will be finalized right before the method exits -- which reclaims the memory.

Henry
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:
... Java 6 added "escape analysis" as an optimization tool...

Wow! I didn't realize that.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic