implicit 'return this' JCP suggestion

Scott Morgan scott at
Wed Jul 21 23:22:23 UTC 2021

Hi All,

  I have a JCP suggestion (between my first and 2nd signatures below), and
I am wondering if anyone on this list can submit it on my behalf, or if
they are interested in collaboration on it.

Cheers and TIA!,



  Implicit return of this;

  Provide better syntactic sugar for method chaining in Java, through
allowing the 'this' keyword to be used

in the return class slot of Java methods.  In addition methods marked as
returning 'this' would implicitly return

this with out the explicit 'return this;' statement at the end of the
method.  For example;

public interface MyInterface {

  this setFoo(String foo);


public class MyClass implements MyInterface {

  private String foo;

  public this setFoo(String foo) { = foo;

    //note the implicit return this;



public class MyChildClass extends MyClass  {

// NO NEED FOR THIS METHOD NOW YEA, it now returns MyChildClass!

//  public MyChildClass setFoo(String foo) {

//    super.setFoo(foo);

//    return this;

//  }




  The primary goal is to make Java easier to use, less typing required and
improved readability.

Removal of repetitive code.



  This is not designed to promote method chaining, as that is subjective.
However for the developers

who like method chaining, it should improve their experience with Java.

Success Metrics


  Take a vote of JCP members to see if they think this is a good idea, for

developer productivity with the language.



  I have been spending a lot of time overriding methods in child classes in
order to

make all of the methods return the same type.  This creates a lot of nearly

code, violating the DRY principles (don't repeat yourself).  The only
difference is the

return type in the leaf most class of the class parent child tree.



I'm hoping someone else will implement this JEP.  However I think

it would be fairly straight forward step in the java compilers.

When the compiler encounters a 'this' in the return type slot

it would return the current class.  For all subclasses and

implementations the compiler would have bytecode generated that would

call the super method and then return this.



I haven't considered any alternatives.  The current work around is pretty



  The parent child classes in the Summary of this JEP should suffice.

Risks and Assumptions


  I don't see any risks.  I am assuming that this will NOT require a change
to any bytecode,

only a change to the language.



  This would require a change to all Java compliers.

Scott Morgan
President & CEO
Adligo Inc
A+ Better Business Bureau Rating

By Appointment Only:
1-866-968-1893 Ex 101
scott at
Send Me Files Securely:

More information about the discuss mailing list