---
title: "How do I call one constructor from another in Java?"  
description: "How do I call one constructor from another in Java?"  
author: "Anonymous User"  
published: 2015-05-12  
updated: 2015-05-12  
canonical: https://www.mindstick.com/forum/23211/how-do-i-call-one-constructor-from-another-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# How do I call one constructor from another in Java?

Is it possible to call a constructor from another (within the same [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp), not from a subclass)? [If yes](https://www.mindstick.com/forum/159589/can-we-crash-jvm-if-yes-then-how) how? And what could be the best way to call another constructor (if there are several ways to do it)?

## Replies

### Reply by Anonymous User

[Yes](https://www.mindstick.com/forum/157763/how-to-return-yes-no-based-on-date-comparison-in-sql), it is possible:

```
public class Foo{    private int x;    public Foo()    {        this(1);    }    public Foo(int x)    {        this.x = x;    }}
```

To chain to a particular superclass constructor instead of one in the same class, use super instead of this. Note that you can only chain to one constructor, and it has to be the first statement in your constructor body.\
In Java another constructor of the same class can be called from a constructor via this(). Note however that this has to be on the first line.\

```
public class MyClass {  public MyClass(double argument1, double argument2) {    this(argument1, argument2, 0.0);  }  public MyClass(double argument1, double argument2, double argument3) {    this.argument1 = argument1;    this.argument2 = argument2;    this.argument3 = argument3;  }}
```

That this has to appear on the first line looks like a big limitation, but you can construct the arguments of other constructors via static methods. For example:\

```
public class MyClass {  public MyClass(double argument1, double argument2) {    this(argument1, argument2, getDefaultArg3(argument1, argument2));  }  public MyClass(double argument1, double argument2, double argument3) {    this.argument1 = argument1;    this.argument2 = argument2;    this.argument3 = argument3;  }  private static double getDefaultArg3(double argument1, double argument2) {    double argument3 = 0;    // Calculate argument3 here if you like.    return argument3;  }}
```

\
There is also the "**Instance initialization block**" that gets executed always and before any other constructor is called. It consists simply of a block of statements "{ ... }" somewhere in the body of your class definition. There is also a "static"version of this to initialize static members: "static { ... }"\
\


---

Original Source: https://www.mindstick.com/forum/23211/how-do-i-call-one-constructor-from-another-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
