# Can a class method return the current instance of its class?

**URL:** https://discourse.processing.org/t/can-a-class-method-return-the-current-instance-of-its-class/12690
**Category:** Coding Questions
**Created:** [July 12, 2019, 8:32pm UTC](https://discourse.processing.org/t/can-a-class-method-return-the-current-instance-of-its-class/12690 "2019-07-12T20:32:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)
#### Post date: [July 12, 2019, 8:32pm UTC](https://discourse.processing.org/t/can-a-class-method-return-the-current-instance-of-its-class/12690/1 "2019-07-12T20:32:53Z")

</div>

Like `return this;` or `return new ClassName(this)` or something like that…

Of course I tried and it didn’t work, so I thought I’d ask if it’s possible.

I’ve made a new Line class, and the following seems to work, I was just hoping for a shortcut to avoid having to call a constructor.

```auto
Line getLine() {
      return new Line(x1, y1, x2, y2);
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 12, 2019, 8:43pm UTC](https://discourse.processing.org/t/can-a-class-method-return-the-current-instance-of-its-class/12690/2 "2019-07-12T20:43:13Z")

</div>

> [@ddownn](#):
>
> , I was just hoping for a shortcut to avoid having to call a constructor.

You can use **clone()** in place of `new`: 👾

> [@Problema con Class, extend y herencia](https://discourse.processing.org/t/problema-con-class-extend-y-herencia/11204/4):
>
> I didn’t need to translate anything, nor use Google Translate. wink Like I had already explained, you’re gonna need to implement clone() in your own class, rather than relying on the [new operator](https://processing.org/reference/new.html): So instead of: Ball hijo() { return new Ball(); } Go w/ something like this: Ball hijo() { return clone(); } @Override Ball clone() { try { final Ball hijo = (Ball) super.clone(); hijo.location = location.get(); return hijo; } catch (final CloneNotSupportedException e) {…

---

<div class="post-metadata">

### Author: ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)
#### Post date: [July 12, 2019, 9:25pm UTC](https://discourse.processing.org/t/can-a-class-method-return-the-current-instance-of-its-class/12690/3 "2019-07-12T21:25:15Z")

</div>

Nice, thanks for the explanation in the link
