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


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

why does this work?

From dkoleary <dkoleary@olearycomputers.com>
Newsgroups comp.lang.java.programmer
Subject why does this work?
Date 2012-08-08 10:30 -0700
Organization http://groups.google.com
Message-ID <aa374d3f-d1d4-43e0-be0a-972b80316255@googlegroups.com> (permalink)

Show all headers | View raw


Hi;

New java programmer.  So new, in fact, that I'm still working my way through the O'Reilly Head First Java book.  One of the end of chapter questions involves identifying if a sample class will compile and what to do to make it compile if it won't.  

The sample class from chapter 4 is:

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

   int go(int arg)
   {  return arg * 2;   }
}

The book says that it'll compile and run, displaying "42 84" and, sure enough, it does:

$ javac XCopy.java
$ java XCopy
42 84

How come that isn't recursive?  XCopy.main() instantiates a new XCopy.  Shouldn't that new XCopy instance also instantiate a new XCopy?  

I was figuring this would run until the XCopy.go function tried returning a number that wouldn't fit in int anymore... That's obviously not the case, but I don't know why.

Can someone provide the missing concept?

Thanks.

Doug O'Leary

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


Thread

why does this work? dkoleary <dkoleary@olearycomputers.com> - 2012-08-08 10:30 -0700
  Re: why does this work? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-08-08 10:38 -0700
    Re: why does this work? dkoleary <dkoleary@olearycomputers.com> - 2012-08-08 11:39 -0700
      Re: why does this work? Lew <lewbloch@gmail.com> - 2012-08-08 17:33 -0700
  Re: why does this work? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-08-08 14:03 -0400
  Re: why does this work? Roedy Green <see_website@mindprod.com.invalid> - 2012-08-09 02:41 -0700
    Re: why does this work? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-08-09 20:17 +0000

csiph-web