• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance variables  RSS feed

 
yaswanth yash
Greenhorn
Posts: 12
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when i run this class the instance variable value will be modified.
At line 1, an instance variable of 4 bytes memory is created & assigned a value of 10.
So, At line 2, are we allocating 4 bytes memory again for variable 'x' ? Is that a disadvantage of Instance variables ?

[See UseCodeTags]
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that won't compile at all, because you can't have executable statements in the class body. Those last two lines ought to be in a method.

But once that is fixed - you'd be overwriting the same variable in memory, so no more memory would be allocated.
 
Vimal Kumar Venugopal
Ranch Hand
Posts: 43
Eclipse IDE Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yaswanth,

I see you are troubling yourself. Let me come to your help.

Well, no memory is going to be allocated for instance variable on re-assigning! The memory allocation remains same. Only the content of the memory changes from 10 -> 20.

 
yaswanth yash
Greenhorn
Posts: 12
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnq so much 4 d replies...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!