• Post Reply Bookmark Topic Watch Topic
  • New Topic

Whats the difference  RSS feed

 
Martin vanPutten
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whats the difference between declaring a int value inside braces and this: public void setPrice(int newPrice)
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

inside braces? You mean inside a class as this:





then k is a member, and i is a local variable.
But I'm not sure if this was what you want to know. Perhaps look at the Language specification, 4.12.3 Kinds of Variables.


Yours,
Bu.
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin,

as Bu mentions, the k variable is an instance variable which means
it follows the object throughout the objects lifetime and can be
accessed everywhere in the object.

i on the other hand only lives within the setPrice method. When you return
from the method the i variable will "die" and can be collected by the
garbage collector.

edit:
if you, on the other hand, means: what's the difference between i and k
in the following example:


within the method there's no difference between the two variables, however
i is already initialized (ie. it has a value) when it enters the method -
this however might not be true if i was an object and not a simple type
(e.g. public void test(MyObject i) could have been invoked with test(null))
[ September 28, 2006: Message edited by: Svend Rost ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!