473,383 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Scope And Persistance of Variables in a Method

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
6 2186
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
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
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


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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Joe Molloy | last post by:
Hi, I'm wondering is there any way I can get a variable's value from within a class when the variable has been declared outside the class but in the same script as the class is contained in. ...
2
by: JJ | last post by:
Hi, I am trying to understand the lifetime or scope of a class in this project. Here is the code that I am talking about: private void PopulateCategoryCombo() { ListItem objListItem;
6
by: Wescotte | last post by:
I'm having an issue where what should be global variables are not in scope and I'm confused as to why. In Case 1 the variables are not in scope for functions in File2 In Case 2 the varibales are...
45
by: bigdadro | last post by:
I've created a new class using prototype.js. After I make the ajax.request all references to this.myClassMethodorVariable are lost. Does the ajax method blow out the object persistance? I'm fairly...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
7
by: surfrat_ | last post by:
Hi, My project has about 4 source files (xxx.cs) and I am having a problem with scope between the files. If I put the code all within one class everything works OK. Can you please point me to...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
9
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.