Is A New Object Instantiated Every Time This Loop Runs?

30
February 13, 2018, at 4:34 PM

I was taking a Java test today and I got this weird question and I feel like I am right, but some of my classmates are saying another answer is right. There was a basic class, a default constructor that initialized the instances, and some methods. In the main class, there was some code like this (not exact because I do not remember it. Let's say the written class name was "tester."

public class Testing {
    public static void main(String[] args) {
        int x = 0;
        Tester t = new Tester ();
        Tester a;
        do {
            new Tester (); 
            x++;
        }while (x < 10);    
    }    
}

And the question was, how many objects are instantiated once the program is completely executed.

My logic was, there was one object instantiated outside the loop. Inside the loop, there was another object, but each time the loop ran, that one object just got re-instantiated. The loop didn't create a new object. So, I said only two objects would be have been instantiated after the program was done running, but some of my classmates said it was 11. Technically, what is correct?

Answer 1

To test this, you can have a static int counter declared in your Test class, and increment the count of the static counter in the constructor and print the value.

Check the value of the counter after this execution.

By the way.. it creates 11 objects.. give it a try.

Answer 2

The line new Tester() does several things

  1. Allocates enough memory to store an instance of Tester
  2. Calls the constructor (which sets up default values)
  3. Returns a reference pointer to the new object, so you could assign it like you did for t

These steps were executed once for t and again for each loop execution, a total of 11 times that a Tester was "instantiated." So your classmates were right.

When the garbage collector runs, each instance of Tester will be deleted. The ones in the loop because they weren't saved in variables. The one stored in t can be deleted once the routine exits.

This is why you should be cautious when you invoke new xyz() for any class, especially in a loop. It is often expensive. Especially the allocation step. Of course it is a tradeoff.

This is a great example of the computer doing exactly what you asked it to do. What you expected it to do was closer to:

public static void main(String[] args) {
    int x = 0;
    Tester t = new Tester();
    Tester a = new Tester();
    do
    {
        a.Reinitialise(); 
        x++;
    }
    while (x < 10);
}

Assuming here that you created a method Reinitialise inside Tester.

Answer 3

11 is the correct answer...whenever you call new, it will create and instantiate objects...

Tester a;

does not instantiate an object...it merely marks that objects assigned to this variable should be of tester type

Best to observe would be to write a print statement in the constructor and then just counting up the number of prints...

READ ALSO
Notification in background

Notification in background

When I open the app and send notification from the FireBase console , the onMessageReceived works fineThe app has its small icon , Notification sound ,etc

62
How to display cards in a Card Game in Java and have them clickable for selection?

How to display cards in a Card Game in Java and have them clickable for selection?

I am programming a game of cards in Java, and everything is going well, but I have come up with a problem regarding the user interfaceThere are 4 players (1 Human 3 AI) and 54 cards

50
polynomial roots in finite fields java or c++ library

polynomial roots in finite fields java or c++ library

Is there any library, java or c++, for working on polynomials in finite fields, for example finding the roots of a polynomial in a finite field? The finite field can be Zp and p may be a very large prime number

47