Erik Movsesyan

Erik Movsesyan

  • 2.1k
  • 9
  • 240

Object being created and destroyed in a forloop

May 14 2022 8:19 PM

Hello, I have the following code here:

class uno {
    int x;

    public uno(int i) {
        x = i;

    public void gen(int h) {
        new uno(h);

class dos {
    public static void Main() {
        uno jj = new(0);

        for (int i=1;i<=10;i++) {


I read in the following book:

pages 134-135

that the gen(int i); method creates and destroys objects immediately.

Does this mean that in a loop whwnwver you create objects after each itertion, the objetc is created and detroyed immediately?
If yes, then how is that object destroyed(obviously not by the GC as It says that the object is destroyed IMMEDIATELY)?


Answers (4)