Scope And Persistance of Variables in a Method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hal Vaughan

    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
  • Raymond DeCampo

    #2
    Re: Scope And Persistance of Variables in a Method

    Hal Vaughan wrote:[color=blue]
    > 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[/color]

    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.prin tln(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.prin tln("s.total = " + s.total);
    System.out.prin tln("t.total = " + t.total);
    System.out.prin tln("instanceCo unt = "
    + instanceCount);
    System.out.prin tln("s.doIt(5)" );
    s.doIt(5);

    }
    }

    HTH,
    Ray

    --
    XML is the programmer's duct tape.

    Comment

    • Hal Vaughan

      #3
      Re: Scope And Persistance of Variables in a Method

      Raymond DeCampo wrote:
      [color=blue]
      > Hal Vaughan wrote:[color=green]
      >> 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[/color]
      >
      > 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.[/color]

      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

      Comment

      • Liz

        #4
        Re: Scope And Persistance of Variables in a Method


        "Hal Vaughan" <hal@thresholdd igital.com> wrote in message
        news:za6dnfijU7 uTGk7dRVn-jA@comcast.com. ..[color=blue]
        > 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[/color]
        variables[color=blue]
        > 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?[/color]
        Could[color=blue]
        > someone clarify this for me?
        >
        > Thanks!
        >
        > Hal[/color]

        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.


        Comment

        • kevinc

          #5
          Re: Scope And Persistance of Variables in a Method



          Hal Vaughan wrote:[color=blue]
          > Raymond DeCampo wrote:
          >
          >[color=green]
          >>Hal Vaughan wrote:
          >>[color=darkred]
          >>>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[/color]
          >>
          >>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.[/color]
          >
          >
          > 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[/color]
          No, the (instance ) variables aren't new with each method invocation.
          They are new upon creation of the object.

          Comment

          • Owen Jacobson

            #6
            Re: Scope And Persistance of Variables in a Method

            On Sat, 19 Jun 2004 12:51:07 +0000, kevinc wrote:
            [color=blue]
            >
            >
            > Hal Vaughan wrote:[color=green]
            >> Raymond DeCampo wrote:
            >>
            >>[color=darkred]
            >>>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.[/color]
            >>
            >>
            >> 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?[/color][/color]

            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.

            Comment

            • kevinc

              #7
              Re: Scope And Persistance of Variables in a Method



              Owen Jacobson wrote:[color=blue]
              > On Sat, 19 Jun 2004 12:51:07 +0000, kevinc wrote:
              >
              >[color=green]
              >>
              >>Hal Vaughan wrote:
              >>[color=darkred]
              >>>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
              >>>>>variable s
              >>>>>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
              >>>>invocatio n. 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?[/color][/color]
              >
              >
              > 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.
              >[/color]
              Actually, in your foo example
              public static int first
              is seen by ALL instances of this class.

              Comment

              Working...