Calling one constructor from another, overloaded in Java

Viewed 111

I was reading this post:

How do I call one constructor from another in Java?

call one constructor from another in java

But I don't know what is the problem whit my code:

NOTE: Only I'm using one call of constructor of three... the error message is indicated like message using // or /*...*/...

class SomeClass {
  private RandomAccessFile RAF = null;

  public SomeClass(String theName) {
    try {
      RandomAccessFile raf = new RandomAccessFile(theName, "r");
      this(raf); //call to this must be first statement in constructor
      SomeClass(raf); /*cannot find symbol
                        symbol:   method SomeClass(RandomAccessFile)
                        location: class SomeClass*/
      this.SomeClass(raf); /*cannot find symbol
                        symbol: method SomeClass(RandomAccessFile)*/
    } catch (IOException e) {}
  }

  public SomeClass(RandomAccessFile RAFSrc) {
    RAF = RAFSrc;

    //...
  }

  //...
}

What's the problem?

2 Answers

When you call one constructor inside another one, it must be the first instruction.

class SomeClass {
  private RandomAccessFile RAF = null;

  public SomeClass(String theName) {
    this(new RandomAccessFile(theName, "r")); 
  }

  public SomeClass(RandomAccessFile RAFSrc) {
    RAF = RAFSrc;

    //...
  }

  //...
}
Related