-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!

Piro
  • 1,367
  • 2
  • 19
  • 40
Alireza Mohamadi
  • 511
  • 5
  • 19
  • 8
    Use `this(10);` in your second constructor instead – StormeHawke Aug 21 '13 at 13:41
  • 1
    Google is your friend. Googling for "chain constructors in java" shows the answer as its very first link. – JB Nizet Aug 21 '13 at 13:43
  • Have you solved the problem? – Vignesh Vino Aug 21 '13 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 – Isaac Sep 05 '17 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. – JB Nizet Sep 06 '17 at 05:22

2 Answers2

12

Use

public FastReader(String b) {
   this(10);
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
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'.