Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java method call chaining in static context

In StringBuilder class I can do like this:

StringBuilder sb = new StringBuilder();
sb.append( "asd").append(34);

method append returns StringBuilder instance, and I can continuosly call that.

My question is it possible to do so in static method context? without class instance

like image 731
Arkaha Avatar asked Dec 18 '10 11:12

Arkaha


1 Answers

Yes. Like this (untested).

public class Static {

  private final static Static INSTANCE = new Static();

  public static Static doStuff(...) {
     ...;
     return INSTANCE;
  }

  public static Static doOtherStuff() {
    ....
    return INSTANCE;
  }
}

You can now have code like.

Static.doStuff(...).doOtherStuff(...).doStuff(...);

I would recommend against it though.

like image 52
Thorbjørn Ravn Andersen Avatar answered Oct 13 '22 20:10

Thorbjørn Ravn Andersen