Peter Ward Peter Ward - 4 months ago 14
Java Question

Why return a class instance inside of a class instance in Java?

I come from a JavaScript and Node.js 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 Spring tutorial and right away came across some syntax that baffled me.

The first walkthrough is a hello world application. First, we set up a Greeting class:

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;
}
}


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:

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));
}
}


In this file, I sort of get that
public Greeting greeting
creates a new instance of the Greeting class, but it only has one argument when the constructor has two,
long id
and
String content
. Next, they return another instance of the class, this time with arguments that match the constructor's structure.

Why is this?

Answer

First I recommend you to learn the Java programming language, without Spring:

With

public class Greeting {

you're defining a class with name Greeting. That's your class.

Inside this class you have:

public Greeting(long id, String content) {

This is the constructor of the class. In Java there aren't objects without calling their constructor. So in order to create a new object you have to call its constructor.

Then you have

public class GreetingController {

This defines another class, in this case GreetingController.

This class has a method (function):

 public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name)

This method receives a string called name and returns an object (instance) of Greeting. In order to return the instance of Greeting you have to create the instance first and this is made calling its constructor:

return new Greeting(counter.incrementAndGet(),
            String.format(template, name));

This line is calling the constructor and passing two parameters, the same that you defined before in the Greeting class' constructor.