public class A extends B {
private A(B b) {
super(b);
}
public static A parse(string s) // question 1
{
B result = D.parse(s);
return new A(result); // question 2
}
}
Question 1: I don’t understand what type of constructor(?) is this.
What I think of a regular constructor looks something like public A(). But this one:
public static A parse(string s)
How should I interpret it?
Question 2: new A(result);
What does this “new” do when returning something?
Is this method returning and calling a private constructor?
Question 1: It’s not a constructor at all, it’s just a static method that creates an object.
Question 2:
newis used to call the constructor on a class.new A(...)calls theAconstructor passing in the arguments.So what you have there in
Ais a class that cannot be instantiated in the normal way (because the constructor is private), but from which you can get instances by callingA.parse.parseis usually called a “factory” method in this case.