driver.manage().window().maximize();
Asked
Active
Viewed 474 times
1
-
Possible duplicate of [How does chained methods execute in java?](https://stackoverflow.com/questions/35932711/how-does-chained-methods-execute-in-java) – Slaw Oct 14 '19 at 07:55
-
2I think this isn't a duplicate. I think he knows how it works, but he wants to know how such a construct is called (i.e. the specific term such a construct is designated with). Eventually he needs this to feed Google with the correct search keywords. – Binarus Oct 14 '19 at 08:28
3 Answers
2
This is known as method chaining, as stated in Thibstars' answer. In your case it's a more concise way of writing:
WebDriver driver = ...; // get instance from somewhere
WebDriver.Options options = driver.manage();
WebDriver.Window window = options.window();
window.maximize();
Note: The class types are assumed based on the selenium tag and method names.

Slaw
- 37,820
- 8
- 53
- 80
1
Each call of the method usually returns the same object which has been calling it (like return this
), allowing to call other methods on the same object. It can however return other objects which can also be method-chained or even void if it is a terminal method.

robingood
- 319
- 2
- 10
-
-
I have said "like" as in "like, as example". this is most commonly used, however other objects or even void (as terminal method call) is employed. https://stackoverflow.com/questions/1103985/method-chaining-why-is-it-a-good-practice-or-not – robingood Oct 14 '19 at 07:58
-
Well, you wrote "Each call of the method returns the _same_ object which has been calling it" - that's actually not necessary. Methods could as well return different objects which might be used to "force" a certain "path" through the chain - e.g. in the OP's example `driver.manage()` will most likely return something different than the call to `.window()`. – Thomas Oct 14 '19 at 08:06
-
I apologize if I'm reading your answer wrong. However, you saying "like" implies, in my opinion, that "`return this`" is an _example of how to implement_ the concept explained in the rest of your answer. The rest of your answer very clearly focuses on "[returning] the same object which has been calling it" which allows calls to "other methods on the same object". The point I was trying to make in my comment was that returning the same object _is not required_. The only important thing is that _an_ object of _any type_ is returned. – Slaw Oct 14 '19 at 08:08