1

This code initializes a new EventBuilder object and sets values on the object. Why is .build() necessary at the end?

tracker.send(new HitBuilders.EventBuilder()
    .setCategory("Achievement")
    .setAction("Unlocked")
    .setLabel("5 Dragons Rescued")
    .setValue(1)
    .build());
Brennan
  • 169
  • 8
  • 3
    Possible duplicate of [When would you use the Builder Pattern?](https://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern) – SoroushA Aug 15 '17 at 16:55

1 Answers1

5

You are creating an instance of a HitBuilders.EventBuilder. Usually — particularly in Android-related development — when you see a class end in ...Builder, it employs the builder pattern. And, when the ...Builder class is a static class inside something else, the builder builds an instance of the outer class. In this case, presumably HitBuilders.EventBuilder creates an instance of HitBuilders.

build() is the convention for a builder class to create the requested instance.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491