1

How is this code working ?

        ConfigurationBuilder cb = new ConfigurationBuilder();
              cb.setDebugEnabled(true)
                .setOAuthConsumerKey("")
                .setOAuthConsumerSecret("")
                .setOAuthAccessToken("")
                .setOAuthAccessTokenSecret("");

Shouldn't we have to put something like

cb.setOAuthConsumerKey("") etc..

Taken from here

7 Answers7

12

Those methods are designed to allow method chaining.

To do so, they simply return this.

For example like this :

public ConfigurationBuilder setDebugEnabled(boolean debugEnabled) {
    this.debugEnabled = debugEnabled;
    return this;
}

This pattern is, today, rather popular because it allows for a slightly less verbose code.

Using it too much in Java leads to a problem, though : as Java methods can return only one value, you lose the possibility to return more useful values and, in practice, you end up with an inconsistent code in which most but not all methods follow this pattern. It can still be practical, especially for building and configuring objects you initially define directly as parameters of your class, but you should be careful not to overuse it.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Can u give me some sample code or link , so I can understand it better? Also, please explain the reason for the down vote. –  Jul 26 '13 at 17:43
3

This is a simple version of the 'builder pattern'. Each method returns this so the caller can chain together many invocations without having to supply the object name many times.

Daniel Renshaw
  • 33,729
  • 8
  • 75
  • 94
1

Each of those methods returns the original reference after changing something, which enables you to chain the methods like this.

Joey
  • 344,408
  • 85
  • 689
  • 683
1

This is an example of splitting one line of code into multiple lines in the source file, even though it's still technically a single line of code.

Without line breaks, it looks like this:

cb.setDebugEnabled(true).setOAuthConsumerKey("").setOAuthConsumerSecret("").setOAuthAccessToken("").setOAuthAccessTokenSecret("");

The reason the multi-line version works is because a carriage return isn't a statement terminator in Java. (It is in some languages, such as VB.) So until a semi-colon is encountered, the compiler continues to execute the code as a single statement.

The code is split into multiple lines purely for readability. (Remember that code is written primarily to be read by humans, and has only a minor secondary purpose of being executed by machines.)

David
  • 208,112
  • 36
  • 198
  • 279
0

This is known as "Builder pattern". Each call to method returns the object instance, and because of this you can write many calls to such methods in "chain".

Display Name
  • 8,022
  • 3
  • 31
  • 66
0

Check the javadoc if available. I suspect that the setters have a return type of ConfigurationBuilder rather than void. In other words, the setters end like this:

return this;
Tap
  • 6,332
  • 3
  • 22
  • 25
0

Method chaining

class Demo{

   public Demo someMethod(){
       //some operations 
       return this; //returns the instance of self
   }

}

This pattern is most commonly used in Builder Pattern.

This is done to provide a concise single line call to configure the object and create the instance of the object in Builder.

Rather than this type of call:

obj.method1();
obj.method2();
obj.method3();

The code gets changed to a single line and readable format:

obj.method1().method2().method3();
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120