How to call one constructor from another in Java

0 votes
Is it possible to call one constructor from another constructor?
May 15, 2018 in Java by Daisy
• 8,140 points
1,579 views

2 answers to this question.

0 votes

You can only chain to one constructor, and it has to be the first statement in your constructor body.

public class Constr {
    private int x;

    public Constr() {
        this(1);
    }

    public Constr(int x) {
        this.x = x;
    }
}
answered May 15, 2018 by Parth
• 4,640 points
0 votes
public class Cons {
 public static Cons newCons(int arg1,...) {
  // This function is commonly called valueOf, like Integer.valueOf(..)
  // More recently called "of", like EnumSet.of(..)
  Cons c = new Cons(...);
  c.setArg1(....);
  return c;
 }
} 


To call a super class, use super(asdf).
answered Jul 24, 2018 by Akrati
• 3,190 points

Related Questions In Java

0 votes
2 answers

How can I Copy files from one directory to another in Java

Java 8 Path sourcepath = Paths.get("C:\\data\\temp\\mydir"); ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
4,447 views
0 votes
1 answer

How to read text files from the Classpath in Java?

InputStream in = this.getClass().getClassLoader().getResourceAsStream("TextFile.txt"); InputStream in = this.getClass().getResourceAsStream("/TextFile.txt"); package ...READ MORE

answered May 8, 2018 in Java by Akrati
• 3,190 points
2,851 views
0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,190 points
5,359 views
0 votes
1 answer

In Java, how to find out the min/max values from array of primitive data types?

import java.util.Arrays; import java.util.Collections; import org.apache.commons.lang.ArrayUtils; public class MinMaxValue { ...READ MORE

answered Jun 12, 2018 in Java by Daisy
• 8,140 points
1,663 views
0 votes
1 answer

Can Abstract Class have a Constructor?

Yes, an abstract class can have a ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,370 points
1,190 views
0 votes
1 answer

How to set a timer in Java?

To work on timer in java, you ...READ MORE

answered May 16, 2018 in Java by Daisy
• 8,140 points
1,231 views
0 votes
2 answers

When and how to use Super() keyword in Java?

super() is used to call immediate parent. super() can be ...READ MORE

answered Jul 9, 2018 in Java by Sushmita
• 6,920 points
1,883 views
0 votes
1 answer

Use of Static Keyword in a Class

It means that there is only one ...READ MORE

answered May 29, 2018 in Java by Rishabh
• 3,620 points
672 views
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,140 points
2,582 views
0 votes
2 answers

How to round any number to n decimal places in Java?

new BigDecimal(String.valueOf(double)).setScale(yourScale, BigDecimal.ROUND_HALF_UP); will get you a BigDecimal. To ...READ MORE

answered Aug 26, 2019 in Java by Sirajul
• 59,230 points
1,732 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP