By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,666 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,666 IT Pros & Developers. It's quick & easy.

Scope And Persistance of Variables in a Method

P: n/a
Being self taught, this is one thing I've always had trouble with -- I
finally get it straight in one situation and I find I'm not sure about
another.

I have a class that keeps calling an internal method. If I define variables
within the method, I would expect that they're new and clean each time I
call the method. If I'm calling from within the class, is that true? Or
should I re-initialize the variables each time the method is called? Could
someone clarify this for me?

Thanks!

Hal
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hal Vaughan wrote:
Being self taught, this is one thing I've always had trouble with -- I
finally get it straight in one situation and I find I'm not sure about
another.

I have a class that keeps calling an internal method. If I define variables
within the method, I would expect that they're new and clean each time I
call the method. If I'm calling from within the class, is that true? Or
should I re-initialize the variables each time the method is called? Could
someone clarify this for me?

Thanks!

Hal


Hal,

Variables that are declared within a method are unique to each method
invocation. They are not shared by a class instance or by different
threads, etc.

Variables declared at the class level are shared by a single class
instance; different threads accessing the same instance will share the
variables.

Variables declared as static at the class level are shared by everybody.
These are essentially globals.

To elucidate further:

public class Scope
{
// A static variable is shared by all
private static int instanceCount = 0;

// An instance variable belongs to the
// instance and may be accessed by multiple
// threads using the same instance
private int total = 0;

public Scope()
{
// For ultimate safety, synchronize
// access to the static variable
synchronized (Scope.class)
{
instanceCount++;
}
}

public synchronized void add(int addend)
{
// This method is synchronized to protect
// the total variable. If your program
// can guarantee that the object will not
// be modified by multiple threads, this
// is unnecessary
total += addend;
}

public void doIt(int count)
{
// No synchronization necessary
// Each invocation gets it own i
int i = count;
while (i > 0)
{
System.out.println(i);
i--;
doIt(i);
}
}

public static void main(String[] args)
{
Scope s = new Scope();
Scope t = new Scope();
s.add(2);
t.add(20);
s.add(4);
System.out.println("s.total = " + s.total);
System.out.println("t.total = " + t.total);
System.out.println("instanceCount = "
+ instanceCount);
System.out.println("s.doIt(5)");
s.doIt(5);

}
}

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2

P: n/a
Raymond DeCampo wrote:
Hal Vaughan wrote:
Being self taught, this is one thing I've always had trouble with -- I
finally get it straight in one situation and I find I'm not sure about
another.

I have a class that keeps calling an internal method. If I define
variables
within the method, I would expect that they're new and clean each time I
call the method. If I'm calling from within the class, is that true? Or
should I re-initialize the variables each time the method is called?
Could someone clarify this for me?

Thanks!

Hal


Hal,

Variables that are declared within a method are unique to each method
invocation. They are not shared by a class instance or by different
threads, etc.


So (just to make sure I've got it), even if the method is private and is
only calledwithin the class,the variables are still new each time its
called from within the class, right?

Thanks!

Hal
Jul 17 '05 #3

P: n/a
Liz

"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:za********************@comcast.com...
Being self taught, this is one thing I've always had trouble with -- I
finally get it straight in one situation and I find I'm not sure about
another.

I have a class that keeps calling an internal method. If I define variables within the method, I would expect that they're new and clean each time I
call the method. If I'm calling from within the class, is that true? Or
should I re-initialize the variables each time the method is called? Could someone clarify this for me?

Thanks!

Hal


Local variables in a method need to be initialized before they are used.
This is independent of who calls the method.
If there is any possibility that they are not initialized first, the
compiler will complain about it and make you fix it.
Jul 17 '05 #4

P: n/a


Hal Vaughan wrote:
Raymond DeCampo wrote:

Hal Vaughan wrote:
Being self taught, this is one thing I've always had trouble with -- I
finally get it straight in one situation and I find I'm not sure about
another.

I have a class that keeps calling an internal method. If I define
variables
within the method, I would expect that they're new and clean each time I
call the method. If I'm calling from within the class, is that true? Or
should I re-initialize the variables each time the method is called?
Could someone clarify this for me?

Thanks!

Hal


Hal,

Variables that are declared within a method are unique to each method
invocation. They are not shared by a class instance or by different
threads, etc.

So (just to make sure I've got it), even if the method is private and is
only calledwithin the class,the variables are still new each time its
called from within the class, right?

Thanks!

Hal

No, the (instance ) variables aren't new with each method invocation.
They are new upon creation of the object.

Jul 17 '05 #5

P: n/a
On Sat, 19 Jun 2004 12:51:07 +0000, kevinc wrote:


Hal Vaughan wrote:
Raymond DeCampo wrote:

Hal Vaughan wrote:

Being self taught, this is one thing I've always had trouble with -- I
finally get it straight in one situation and I find I'm not sure about
another.

I have a class that keeps calling an internal method. If I define
variables
within the method, I would expect that they're new and clean each time I
call the method. If I'm calling from within the class, is that true? Or
should I re-initialize the variables each time the method is called?
Could someone clarify this for me?

Thanks!

Hal

Hal,

Variables that are declared within a method are unique to each method
invocation. They are not shared by a class instance or by different
threads, etc.

So (just to make sure I've got it), even if the method is private and is
only calledwithin the class,the variables are still new each time its
called from within the class, right?


class foo {
public static int first; // one 'first' for the lifetime of the class
public int second; // one 'second' for each instance of the class

public void doSomething () {
int third; // new 'third' for *each* invocation of doSomething
}
}

Hope that helps.

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 17 '05 #6

P: n/a


Owen Jacobson wrote:
On Sat, 19 Jun 2004 12:51:07 +0000, kevinc wrote:


Hal Vaughan wrote:
Raymond DeCampo wrote:

Hal Vaughan wrote:
>Being self taught, this is one thing I've always had trouble with -- I
>finally get it straight in one situation and I find I'm not sure about
>another.
>
>I have a class that keeps calling an internal method. If I define
>variables
>within the method, I would expect that they're new and clean each time I
>call the method. If I'm calling from within the class, is that true? Or
>should I re-initialize the variables each time the method is called?
>Could someone clarify this for me?
>
>Thanks!
>
>Hal

Hal,

Variables that are declared within a method are unique to each method
invocation. They are not shared by a class instance or by different
threads, etc.
So (just to make sure I've got it), even if the method is private and is
only calledwithin the class,the variables are still new each time its
called from within the class, right?

class foo {
public static int first; // one 'first' for the lifetime of the class
public int second; // one 'second' for each instance of the class

public void doSomething () {
int third; // new 'third' for *each* invocation of doSomething
}
}

Hope that helps.

Actually, in your foo example
public static int first
is seen by ALL instances of this class.

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.