Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Chaining in Java [closed]

While answering a few questions on here earlier and from some work I have been doing lately I have been wondering why Java does not support method chaining on its built in classes.

If I were to create a Car class for example, I could make it chainable by reutrning this instead of void as follows:

public class Car {     private String make;              public Car setMake(String make) {         this.make = make;         return this;     }    } 

Is there any particular reason why the built in libraries don't tend to do things this way? Is there a downside to method chaining?

I may have overlooked something which would explain the lack of method chaining however any setter method that returns void by default should return a reference to this (at least in my eyes it should). This would make situations like the following much cleaner.

container.add((new JLabel("label text")).setMaximumSize(new Dimension(100,200))); 

rather than the more long winded: Note: It would not stop you from coding this way if you wished.

JLabel label = new JLabel("label text"); label.setMaximumSize(new Dimension(100,200)); container.add(label); 

I would be very interested to hear the reasons behind this decision, If I had to guess it would be that there is an overhead associated with this and so should only be used when needed.

like image 539
Jon Taylor Avatar asked Jul 19 '12 13:07

Jon Taylor


People also ask

Is method chaining possible in Java?

Method chaining in Java is a common syntax to invoke multiple methods calls in OOPs. Each method in chaining returns an object. It violates the need for intermediate variables.

What is method chaining Why is it bad?

It's merely syntactic sugar for where the next method acts on the return value of the previous method. No, it really is called method chaining regardless of the objects that are returned. Method chaining just means chaining multiple method calls into a single expression, rather than using multiple statements.

Is system out Println a chaining method?

No, it's not method chaining. You're right about System being a class (just a regular class, not "static" - only inner classes can be static), but out is a static field of the class (of the type java. io. PrintStream), and only println() is a method of PrintStream .


2 Answers

Eh. There's readability arguments to be made in both directions -- there's such a thing as trying to put too much into a single line.

But honestly, I suspect here it's for historical reasons: pervasive "chaining" behavior hadn't really become popular or well-known when e.g. Swing was being developed. You could argue that it should've been added in later on, but things like that tend to create binary incompatibilities and other issues that Sun/Oracle have historically been extremely cautious about.

More recent JDK libraries -- see e.g. ByteBuffer for a major, well-known example -- have provided chaining behavior and the like, where it makes sense.

like image 136
Louis Wasserman Avatar answered Oct 05 '22 16:10

Louis Wasserman


Another reason I can think of is performance, or more precisely: don't pay for something you don't use. return this after each and every method isn't very costly, but still requires few extra CPU cycles and one CPU registry.

There was even an idea to add implicit return this to every method declaring void return value but it was rejected.

like image 31
Tomasz Nurkiewicz Avatar answered Oct 05 '22 17:10

Tomasz Nurkiewicz