1

I'm learning Spring framework with a tutorial online. However, I have a question about this Java code:

     attributes.addFlashAttribute(ResultMessages.success().add(
             ResultMessage.fromText("Created successfully!")));
     return "redirect:/todo/list";

Full source

What I'm wondering about: Is it 'Class.method.method'? If so, how can it be possible?

Cœur
  • 37,241
  • 25
  • 195
  • 267
LCL
  • 33
  • 7

2 Answers2

7

It's called chaining. the second method is called upon the returned value of the first.

let's say this:

public Person {
  private String name;
  public Person(String name){ this.name = name; }

  public String getName() { return this.name; }
}

if you have an instance p of Person, the next two snippets are the same (in result)

String pN = p.getName().toUpperCase();

and:

   String name = p.getName();
   String pN = name.toUpperCase();
Arnaud
  • 17,229
  • 3
  • 31
  • 44
Stultuske
  • 9,296
  • 1
  • 25
  • 37
1

This is called method chaining or named parameter idiom and it is a syntax for invoking multiple method calls. This is a common feature on many object oriented languages.

Simply speaking, by this syntax you directly invoke a method on the returned value without the need of assignment to a variable object first. One major advantage is the reduction of vertical problem in programming.