Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Instance Variable v.s. Local Variable

Reply
Thread Tools

Instance Variable v.s. Local Variable

 
 
Jerry
Guest
Posts: n/a
 
      07-30-2005
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!

 
Reply With Quote
 
 
 
 
KittyCreation
Guest
Posts: n/a
 
      07-30-2005
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.

 
Reply With Quote
 
 
 
 
KittyCreation
Guest
Posts: n/a
 
      07-30-2005
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.

 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-30-2005
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
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      07-30-2005
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".


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-06-2005
On 29 Jul 2005 23:32:09 -0700, "Jerry" <(E-Mail Removed)> 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/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
including instance methods and setting an instance variable Leon Bogaert Ruby 19 03-23-2008 09:29 PM
Problem when subclass instance changes base class instance variable Gerry Sutton Python 1 04-16-2005 06:06 AM
accessing class instance variable from instance method David Garamond Ruby 5 06-08-2004 02:26 PM
Instance Variable vs Local Variable Paul Carey Java 3 12-03-2003 05:05 PM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments