Instance Variable v.s. Local Variable

J

Jerry

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!
 
K

KittyCreation

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.
 
K

KittyCreation

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.
 
P

Patricia Shanahan

Jerry said:
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
 
T

Thomas G. Marshall

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.
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top