title: Why return a class instance inside of a class instance in Java?
body: <p>I come from a JavaScript and <a href="http://en.wikipedia.org/wiki/Node.js" rel="nofollow">Node.js</a> background, and though I have most OOP basics down, there are many Java basics that I don't quite understand. For instance, I'm doing <a href="http://spring.io/guides/gs/rest-service/#initial" rel="nofollow">a Spring tutorial</a> and right away came across some syntax that baffled me.</p>
  
  <p>The first walkthrough is a hello world application. First, we set up a Greeting class:</p>
  
  <pre><code>public class Greeting {
  
      private final long id;
      private final String content;
  
      public Greeting(long id, String content) {
          this.id = id;
          this.content = content;
      }
  
      public long getId() {
          return id;
      }
  
      public String getContent() {
          return content;
      }
  }
  </code></pre>
  
  <p>To start off with, I don't get why they define an instance of the class within the class itself. Why not just add arguments to the public class? Anyway, that's not my question. Where I get more lost is when we make a Greeting controller:</p>
  
  <pre><code>package hello;
  
  import java.util.concurrent.atomic.AtomicLong;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  @RestController
  public class GreetingController {
  
      private static final String template = "Hello, %s!";
      private final AtomicLong counter = new AtomicLong();
  
      // This ensures that HTTP requests to /greeting are mapped to the greeting() method.
      @RequestMapping("/greeting")
      public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
          return new Greeting(counter.incrementAndGet(),
              String.format(template, name));
      }
  }
  </code></pre>
  
  <p>In this file, I sort of get that <code>public Greeting greeting</code> creates a new instance of the Greeting class, but it only has one argument when the constructor has two, <code>long id</code> and <code>String content</code>. Next, they return another instance of the class, this time with arguments that match the constructor's structure.</p>
  
  <p>Why is this?</p>