BitSet does not have a free interface, so something like new BitSet(4).set(0,3) does not work for BitSets. There are only static methods BitSet.valueOf (), but they are somewhat inconvenient to use. However, if you want a static configuration, you can simply create an instance of BitSet with the desired value, use BitSet.toLongArray (), print the values ββof the array and create an instance of your BitSet. In your specific example, the default constructor could be:
public MyClass() { this(BitSet.valueOf(new long[]{7})); }
As for the general part of the question: it will only work if you have a βsetterβ that returns the current object, which will allow you to bind calls. So for your own classes, you can do something like this:
public class A { private int num; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public A withNum(int num) { setNum(num); return this; } }
If you used this in the constructor, for example, using BitSet, you can do this(new A().withNum(4));
Free interfaces are pretty popular (for example, the AWS SDK has this everywhere), only JDK objects usually don't have them.
source share