Z101 Z101 - 1 month ago 8
Java Question

Why specify a method twice - Java

I am a python coder but recently started a forey into Java. I am trying to understand a specific piece of code but am running into difficulties which I believe are associated with not knowing Java too well, yet.

Something that stood out to me is that sometimes inside class definitions methods are called twice. I am wondering why that is? For example:

The following code is taken from a file called ApplicationCreator.java. I noticed that the public class ApplicationCreator essentially instantiates itself twice, or am I missing something here?

public class ApplicationCreator<MR> implements
IResourceObjectCreator<BinaryRuleSet<MR>> {

private String type;

public ApplicationCreator() {
this("rule.application");
}

public ApplicationCreator(String type) {
this.type = type;
}


So here my questions:

1) Why would the class instantiate itself inside the class?

2) Why would it do so twice? Or is this a way to set certain parameters of the ApplicationCreator class to new values?

Any advice would be highly appreciated.

Answer

1) Why would the class instantiate itself inside the class?

The class is not calling itself, it is proving a way for others to instantiate its object. Read about constructor.

2) Why would it do so twice? Or is this a way to set certain parameters of the ApplicationCreator class to new values?

As I said, it is a way to create object. 1st one will assign default value to type. And 2nd will give others an option to assign a value. Read about constructor overloading.

this in the constructor will call another constructor of the same class depending upon argument type passed to this.