-2

This made me mad (Eclipse Kepler)

public class FastReader 
{
public static void main (String[] args)
{
    FastReader a = new FastReader("hi");
}
public FastReader(int a)
{

}
public FastReader(String b)
{
    FastReader(10);
}
}

And I get this error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method FastReader(int) is undefined for the type FastReader    
at FastReader.<init>(FastReader.java:14)
at FastReader.main(FastReader.java:6)

It almost made me mad! help me get rid of this! Thank you!

5
  • 8
    Use this(10); in your second constructor instead Commented Aug 21, 2013 at 13:41
  • 1
    Google is your friend. Googling for "chain constructors in java" shows the answer as its very first link. Commented Aug 21, 2013 at 13:43
  • Have you solved the problem? Commented Aug 21, 2013 at 13:46
  • 1
    @JBNizet nowhere in the exception is someone able to deduce that they need to look for "chaining constructors". I think this question is very valid, a duplicate for sure, but valid Commented Sep 5, 2017 at 23:41
  • @Isaac I never said the question was invalid. I just said thet, since the OP wants to call a constructor from another constructor in Java, the first thing to do is to do a tiny bit of research by googling, for example "call a constructor from another constructor in Java". Just doing that would have led to the answer immediately. Commented Sep 6, 2017 at 5:22

2 Answers 2

12

Use

public FastReader(String b) {
   this(10);
}
Sign up to request clarification or add additional context in comments.

2 Comments

You say that my way of calling constructors is completely wrong bro? thanks
Yes, the compiler is telling you this, the above is the correct syntax for constructor chaining
2
public FastReader(String b)
{
    this(10);
}

This is the correct way to call the same class constructor. If you want to call a same class constructor use the keyword 'this' if you want call the parent class constructor use the keyword 'super'.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.