-1

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.

codingenious
  • 8,385
  • 12
  • 60
  • 90
Mike Nedelko
  • 709
  • 1
  • 8
  • 28
  • These are called constructors. One takes a parameter and the other doesn't. The one that does then use a hard coded value to construct itself by calling the other constructor. As another `new` keyword is not used, only one Object is instantiated – Scary Wombat Sep 07 '16 at 05:00
  • Oh. Ok so in this example, does that mean that depending on whether I instantiate the class with no arguments or with one argument (type) the code would execute the first or second constructor respectively? – Mike Nedelko Sep 07 '16 at 05:03
  • This is called "overloading", and refers to the ability to define a constructor or method multiple times but with different parameters. The number and type of the parameters when calling the method determines which method is called. – 4castle Sep 07 '16 at 05:03
  • @Z101 Yes, exactly. – Andrew Li Sep 07 '16 at 05:03

6 Answers6

4

It's not instantiating itself in the class, it's calling a different constructor in the class.

What these are are overloaded constructors. Constructors are somewhat method-like, but they are called on object creation. Consider this:

public class Example {
    private int instanceVariable;

    public Example() { //a constructor of Example
        instanceVariable = 3;
        System.out.println("New Example object was created!");
    }

    public static void main(String[] args) {
        Example ex = new Example();
    }
}

Here, we have an Example class which has a constructor. If you look in the main method, we create a new instance of Example. The program will output New Example object was created! and set the instance's instanceVariable to 3 because the constructor is immediately called on object as it constructs the object (hence the name).

Now if you take a look at your situation, the constructors have different arguments (and thus signatures) so the object can be constructed by giving no arguments, or supplying a String. Let me illustrate what this does:

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

this refers to the class in this case, and invoking this(args) calls a constructor of the class. Since we have overloaded constructors, Java will call the constructor that matches most closely to the passed arguments. Since, in this case, a String is passed, Java will see that the constructor public ApplicationCreator(String) is the one that matches most and will invoke it.

Inside the no-argument constructor, it calls the other constructor with String rule.application, so you can think of the no-argument constructor as passing a default value to the constructor taking in a String.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
2

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.

codingenious
  • 8,385
  • 12
  • 60
  • 90
2

These are two different constructors.
They have what is refered to as "different signatures.

Using it you can construct a ApplicationCreator object in two different ways :

ApplicationCreator ac = new ApplicationCreator();

Or

ApplicationCreator ac = new ApplicationCreator("A String");

For further reading see: The Java Class Constructor

c0der
  • 18,467
  • 6
  • 33
  • 65
1

The class is really not instantiating itself twice. Rather, the default constructor ApplicationCreator() (i.e. the one which takes no parameters), is simply calling the constructor which accepts an input string.

This ensures that an ApplicationCreator object will always have a type. When a type is not specified the default value rule.application will be used.

This is an example of overloaded constructors.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

It's called a constructor. And it's not "called twice", one simply redirects to the other via this() with the given parameters.

Essentially the first way, without parameters, simply has a default value. Otherwise, you construct an instance with the given String type

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Here this class has two constructor.
When class name "method" name are same you can understand those are constructor.
Here constructor is over loaded . Based on parameter classes will be instantiated. Here user have a choice based on need .

c0der
  • 18,467
  • 6
  • 33
  • 65