Instance Variable v.s. Local Variable

Discussion in 'Java' started by Jerry, Jul 30, 2005.

  1. Jerry

    Jerry Guest

    What is the difference between instance variable and local variable? Is
    that true instance variables are shared among threads while local
    variable is not shared?

    public class Test
    {
    private String var1; // is this instance variable?
    public int var2; // is this also instance variable?

    private void method1()
    {

    String var3; // is this local variable?

    }
    }

    Thanks a lot!
     
    Jerry, Jul 30, 2005
    #1
    1. Advertising

  2. Yes your comments are correct,
    Instance variables are class level member variables. Local variables
    are defined in a method/block.

    Local variables are not shared amongst thread.
     
    KittyCreation, Jul 30, 2005
    #2
    1. Advertising

  3. Yes your comments are correct,
    Instance variables are class level member variables. Local variables
    are defined in a method/block.

    Local variables are not shared amongst thread.
     
    KittyCreation, Jul 30, 2005
    #3
  4. Jerry wrote:
    > What is the difference between instance variable and local variable? Is
    > that true instance variables are shared among threads while local
    > variable is not shared?
    >
    > public class Test
    > {
    > private String var1; // is this instance variable?
    > public int var2; // is this also instance variable?
    >
    > private void method1()
    > {
    >
    > String var3; // is this local variable?
    >
    > }
    > }
    >
    > Thanks a lot!
    >


    As well as being shared among threads, instance variables are shared
    among method invocations. Changes in var1 or var2 will persist between
    calls to method1, and to any other method that references them, while
    var3's value is lost when method1 returns.

    Patricia
     
    Patricia Shanahan, Jul 30, 2005
    #4
  5. KittyCreation coughed up:
    > Yes your comments are correct,
    > Instance variables are class level member variables.


    "class level member variables."

    Perhaps is better phrased as

    "object level member variables".

    The terms "class" and "member" together (a conflict) might confuse someone
    just learning OO.




    > Local variables
    > are defined in a method/block.
    >
    > Local variables are not shared amongst thread.




    --
    If I can ever figure out how, I hope that someday I'll
    succeed in my lifetime goal of creating a signature
    that ends with the word "blarphoogy".
     
    Thomas G. Marshall, Jul 30, 2005
    #5
  6. Jerry

    Roedy Green Guest

    On 29 Jul 2005 23:32:09 -0700, "Jerry" <> wrote
    or quoted :

    >What is the difference between instance variable and local variable? Is
    >that true instance variables are shared among threads while local
    >variable is not shared?


    see http://mindprod.com/jgloss/instance.html
    http://mindprod.com/jgloss/local.html

    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
     
    Roedy Green, Aug 6, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Carey

    Instance Variable vs Local Variable

    Paul Carey, Dec 3, 2003, in forum: Java
    Replies:
    3
    Views:
    838
    Chris Uppal
    Dec 3, 2003
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,927
    Christian Jan├čen
    Aug 8, 2003
  3. Gerry Sutton
    Replies:
    1
    Views:
    586
    Peter Otten
    Apr 16, 2005
  4. David Garamond
    Replies:
    5
    Views:
    281
    Ara.T.Howard
    Jun 8, 2004
  5. Leon Bogaert
    Replies:
    19
    Views:
    361
    Robert Klemme
    Mar 23, 2008
Loading...

Share This Page