6 Replies Latest reply: Oct 11, 2010 6:05 AM by YoungWinston RSS

    Reference Object and New Key Word

    800745
      Hi,

      I am having a Class call ThreadHandler which implements Runnable()

      I loop through 10 times like below


      for (int i = 0; i < 10; i++) {
      ThreadHandler oTHandler = new ThreadHandler();
      Thread oTh = new Thread(oTHandler);
      oTh.start();
      }

      Question:

      1. Will this create unnecessary referencing to ThreadHandler?
      2. Can I just instantiate ThreadHandler at the attribute declaration and just use new key word every time in the loop

      E.g :
      ThreadHandler oTHandler
      Thread oTh
      for (int i = 0; i < 10; i++) {
      oTHandler = new ThreadHandler();
      oTh = new Thread(oTHandler);
      oTh.start();
      }
        • 1. Re: Reference Object and New Key Word
          801507
          Suppose we say:
          for(ndx = 0; ndx < 1000; ndx++) {
              Type foo = new Type("etc");
              // ...
          }
          // <-- foo is no longer in scope
          I'm not 100% sure what you mean "unnecessary referencing of..." but if you declare inside the for loop then there will be nothing referenced by foo after the loop. So, if anything there is less referencing going on.

          It is considered good practice to declare things close to where you use them. Because your intent is clearer that way.
          • 2. Re: Reference Object and New Key Word
            800745
            So doing ....

            *for(ndx = 0; ndx < 1000; ndx++) {*
            Type foo = new Type("etc");
            *// ...*
            *}*
            *// <-- foo is no longer in scope*


            AND

            Type foo*
            for(ndx = 0; ndx < 1000; ndx+) {+*
            foo = new Type("etc");*
            +// ...+
            +}+
            +// <-- foo is no longer in scope+

            Does not have an impact on performance? Correct?
            • 3. Re: Reference Object and New Key Word
              EJP
              Type foo;
              for(ndx = 0; ndx < 1000; ndx++) {
              foo = new Type("etc");
              // ...
              }
              // <-- foo is no longer in scope
              But 'foo' is in scope here. That's why the former is preferable.
              • 4. Re: Reference Object and New Key Word
                801507
                JLearner wrote:
                Does not have an impact on performance? Correct?
                I mentioned before that I don't really know what you mean by "unnecessary referencing of..." . What is it that you think might lead to an impact on performance?
                • 5. Re: Reference Object and New Key Word
                  796440
                  Declaring a local variable inside or outside the loop makes no difference at execution time. All local variables are allocated in one shot at method entry. Putting a declaration inside or outside the loop doesn't matter--it's still a single variable.

                  In terms of programming style, however, the general rule is to declare a variable in the narrowest scope possible. So if that variable is not used outside the loop, don't declare it outside the loop.
                  • 6. Re: Reference Object and New Key Word
                    YoungWinston
                    JLearner wrote:
                    I loop through 10 times like below
                    for (int i = 0; i < 10; i++) {
                    ThreadHandler oTHandler = new ThreadHandler();
                    Thread oTh = new Thread(oTHandler);
                    oTh.start();
                    }
                    Question:
                    1. Will this create unnecessary referencing to ThreadHandler?
                    No, but even if you get the scope right it might create unnecessary ThreadHandlers. How many do you want? I have no idea what it's supposed to do, but the name suggests that a ThreadHandler can deal with several Threads.

                    Winston