I am new in Java and I am trying to call a constructor inside other constructor. Here is my code:
class TestContructor {
public TestContructor(){
String params = "1, 2, 3";
this(params);
}
public TestContructor(String params){
System.out.println("TestContructor with params: " + params);
}
}
class TestRunner {
public static void main(String[] args) {
new TestContructor();
}
}
Then I got this error:
javac -g Test.java
Test.java:5: error: call to this must be first statement in constructor
this(params);
^
1 error
And ok, I changed the TestConstructor code:
class TestContructor {
public TestContructor(){
// String params = "1, 2, 3";
this("1, 2, 3"); //Now is the first statement
}
public TestContructor(String params){
System.out.println("TestContructor with params: " + params);
}
}
And Ok, no problems.
Then, is it a rule in java to call another constructor it must be declared in the first statement line?
What about if I need to do some kind of process before call the other constructor?
I recall I am newer in java programming, but I have this problem and I want to know if it is possible to do that I want.