Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #17433

Re: why does this work?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!news-in-01.newsfeed.easynews.com!easynews.com!easynews!news-out.news.tds.net!newsreading01.news.tds.net!53ab2750!not-for-mail
From "Eric Sosman" <eric.sosman@1:261/38.remove-r72-this>
Subject Re: why does this work?
Message-ID <5022BA8F.56386.calajapr@time.synchro.net> (permalink)
X-Comment-To dkoleary
Newsgroups comp.lang.java.programmer
In-Reply-To <5022AB86.56377.calajapr@time.synchro.net>
References <5022AB86.56377.calajapr@time.synchro.net>
X-FTN-AREA COMP.LANG.JAVA.PROGRAMMER
X-FTN-MSGID 1:261/38 cfe323fe
X-FTN-REPLY 1:261/38 4be438ba
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.16a-Win32 NewsLink 1.98]
Lines 60
Date Wed, 08 Aug 2012 20:06:28 GMT
NNTP-Posting-Host 69.21.70.65
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1344456388 69.21.70.65 (Wed, 08 Aug 2012 15:06:28 CDT)
NNTP-Posting-Date Wed, 08 Aug 2012 15:06:28 CDT
Organization tds.net
X-Received-Bytes 3451
Xref csiph.com comp.lang.java.programmer:17433

Show key headers only | View raw


  To: dkoleary
From: Eric Sosman <esosman@ieee-dot-org.invalid>

On 8/8/2012 1:30 PM, dkoleary wrote:
> [...]
> How come that isn't recursive?  XCopy.main() instantiates a new XCopy.
Shouldn't that new XCopy instance also instantiate a new XCopy?

     See Daniel Pitts' explanation.  Another thing you could try to
help you see what's going on is to sprinkle some more println() calls through 
the code to help trace through the execution.  In this case you're interested 
in where the constructor fits with relation to everything else, but there's no 
explicit constructor written in the code.  As I'm sure you've learned, this 
means the compiler will write a simple constructor for you -- but there's no 
way to get the compiler to stick println() calls in what it writes, so your 
recourse is to write your own explicit constructor. With this in mind, the code 
might look like:

        class XCopy {
            public static void main(String[] args) {
                System.out.println("entering main()");
                int orig = 42;
                System.out.println("main() creates an XCopy");
                XCopy x = new XCopy();
                System.out.println("main() created an XCopy");
                int y = x.go(orig);
                System.out.println(orig + " " + y);
                System.out.println("main() is finished");
            }

            int go(int arg) {
                System.out.println("executing go(), arg = " + arg);
                return arg * 2;
            }

            // Explicit constructor, just for the println
            XCopy() {
                System.out.println("constructing an XCopy");
            }
        }

Run this version, study the output, and see if the sequence of events becomes 
clearer.

     This technique is sometimes called "printf debugging" (the name
comes from a different programming language).  Despite its simplicity, it can 
be astonishingly effective, and the overall approach can be used in most 
environments and most languages.  Indeed, Java's various logging frameworks 
(you may learn about them later) are basically just fancied-up versions of 
printf debugging: A piece of the program blurts "Look! I'm *here*, and these 
are a few interesting values."

--
Eric Sosman
esosman@ieee-dot-org.invalid

--- BBBS/Li6 v4.10 Dada-1
 * Origin: Prism bbs (1:261/38)
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

why does this work? "dkoleary" <dkoleary@1:261/38.remove-qhs-this> - 2012-08-08 19:04 +0000
  Re: why does this work? "Daniel Pitts" <daniel.pitts@1:261/38.remove-qhs-this> - 2012-08-08 19:04 +0000
    Re: why does this work? "dkoleary" <dkoleary@1:261/38.remove-k2r-this> - 2012-08-09 18:44 +0000
      Re: why does this work? "Lew" <lew@1:261/38.remove-k2r-this> - 2012-08-09 18:44 +0000
  Re: why does this work? "Eric Sosman" <eric.sosman@1:261/38.remove-r72-this> - 2012-08-08 20:06 +0000
  Re: why does this work? "Roedy Green" <roedy.green@1:261/38.remove-k2r-this> - 2012-08-09 18:44 +0000
    Re: why does this work? "glen herrmannsfeldt" <glen.herrmannsfeldt@1:261/38.remove-t9h-this> - 2012-08-10 18:39 +0000

csiph-web