permaculture playing cards*
The moose likes Beginning Java and the fly likes Instance variables and local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance variables and local variables" Watch "Instance variables and local variables" New topic
Author

Instance variables and local variables

Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
Why does Java provide a default value to instance variables and does not do so to local variables (variables declared in a method) ? Is there a difference in the way they are handled?


"I can resist everything except temptation"
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Instance variables represent the object on the heap while local variables are on the stack. To initialize local variables java would have to insert code at the top of every method to update those stack-resident variables, which means a memory write for every local variable. The cummulative effect would be very signficant and it's usually redundant: locals are always set before they are used.
Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
Thanks a lot! for the response
 
 
subject: Instance variables and local variables
 
Similar Threads
Variables defined inside a constructor
Are variables defined inside a constructor instance or local?
Method local variables doesn't get default values
Variables in Java
what is difference between