# Java Builder for Kotlin Class

**URL:** <https://discuss.kotlinlang.org/t/java-builder-for-kotlin-class/1611>\
**Category:** Language Design\
**Created:** [April 19, 2016, 12:23pm UTC](https://discuss.kotlinlang.org/t/java-builder-for-kotlin-class/1611 "2016-04-19T12:23:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [April 19, 2016, 12:23pm UTC](https://discuss.kotlinlang.org/t/java-builder-for-kotlin-class/1611/1 "2016-04-19T12:23:47Z")

</div>

Hi,

in pure Java I often used freebuilder [0] to automatically generate builders from interfaces.

Now, as I convert many of these interfaces into kotlin-classes and using them from Java, I wish I could somehow have a builder from kotlin-constructors, so I can do:

```
new Class()
.setPropA()
.setPropB()
.setPropC()

```

I thought about using setters with default parameters, but they don’t allow me to return `this`.  
Is there some language trick which makes this easier for me?

Fabian Zeindl

[0] [GitHub - inferred/FreeBuilder: Automatic generation of the Builder pattern for Java](https://github.com/google/FreeBuilder)

---

<div class="post-metadata">

**Author:** ![b005t3r](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/b005t3r/32/1921_2.png) [@b005t3r](https://discuss.kotlinlang.org/u/b005t3r)\
**Post date:** [April 19, 2016, 12:25pm UTC](https://discuss.kotlinlang.org/t/java-builder-for-kotlin-class/1611/2 "2016-04-19T12:25:32Z")

</div>

> [@fab1an](#):
>
> Is there some language trick which makes this easier for me?

How about this: [https://kotlinlang.org/docs/reference/idioms.html#calling-multiple-methods-on-an-object-instance-with](https://kotlinlang.org/docs/reference/idioms.html#calling-multiple-methods-on-an-object-instance-with)

---

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [April 19, 2016, 1:14pm UTC](https://discuss.kotlinlang.org/t/java-builder-for-kotlin-class/1611/3 "2016-04-19T13:14:15Z")

</div>

How do I use this from inside Java?

---

<div class="post-metadata">

**Author:** ![b005t3r](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/b005t3r/32/1921_2.png) [@b005t3r](https://discuss.kotlinlang.org/u/b005t3r)\
**Post date:** [April 19, 2016, 1:20pm UTC](https://discuss.kotlinlang.org/t/java-builder-for-kotlin-class/1611/4 "2016-04-19T13:20:48Z")

</div>

Hmm, I don’t think it is possible.

---

<div class="post-metadata">

**Author:** ![fab1an](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fab1an/32/1934_2.png) [@fab1an](https://discuss.kotlinlang.org/u/fab1an)\
**Post date:** [April 19, 2016, 3:52pm UTC](https://discuss.kotlinlang.org/t/java-builder-for-kotlin-class/1611/5 "2016-04-19T15:52:07Z")

</div>

:(.

Is there a good way for using building immutable Kotlin Objects with a lot of members from Java?

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [April 19, 2016, 8:22pm UTC](https://discuss.kotlinlang.org/t/java-builder-for-kotlin-class/1611/6 "2016-04-19T20:22:12Z")

</div>

You (kind of) have to create a separate mutable builder. In Kotlin, you could have a public constructor that takes a configuration closure that has this builder (or an interface for a builder) as parameter. The builder would then be passed so another constructor that uses it to initialise the actual class. It is possible in plain Java to implement such a closure and use it in the same way. Otherwise, go for the more explicit builder pattern.

```auto
interface IBuilder { 
   var x:Int
   fun withX(val:Int):IBuilder
   var y:Int
   fun withY(val:Int):IBuilder

   fun build():MyClass
   /*....*/
}
private class MyBuilder:IBuilder { /*......*/ }

class MyClass private constructor(myBuilder: MyBuilder) {
  companion object { // This below is only to be nicer to pre-8 Java, post-8 Java should hapily use a lambda
    @JvmStatic
    fun builder:IBuilder() = MyBuilder()
  }

  val x = myBuilder.x
  val y = myBuilder.y

  constructor(config:IBuilder.()->Unit):this(MyBuilder.apply(config))
}

fun makeMyClass() {
  MyClass { x=5; y=10 }
}

```

In Java 6/7

```nohighlight
static void makeMyClass() {
  MyClass foo = MyClass.builder.withX(5).withY(10).build()
}

```
