• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Whats the difference

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the difference between declaring a int value inside braces and this: public void setPrice(int newPrice)
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic