wood burning stoves 2.0*
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance variables and local variables" Watch "Instance variables and local variables" New topic

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
jQuery in Action, 2nd edition
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