0

I'm new to java. I came through the following code while reading an introductory book on Java while reading about the finalize method.

class FDemo{ int x;
FDemo(int i){
  x=i;
}
protected void finalize(){ 
 System.out.println("Finalizing " +x)
  }
void generator(int i) {
 FDemo o= new FDemo(i);
 }
}

Now while creating objects I encountered the following in the new class

  int count;

FDemo ob= new FDemo(0);

for (count=1;count<10000000;count++)
ob.generator(count);

I'm having trouble understanding how are we creating new objects using the same name 'o' in the for loop? Would we have to create a new object and then on the next iteration discard the existing object 'o' throughout the loop? Also, why does finalize execute for 10 million but not for 1000 if we are constantly creating and discarding the same object? Like I said, I'm a beginner and this question might sound very basic or even silly for most of the people here but I would appreciate it very much if you could take out a few minutes to answer. Thank you

Dipanshu Juneja
  • 1,204
  • 14
  • 29
  • Just curious, are you reading Java- A Beginner's Guide by Herbert Schildt? – Ashwin Gupta Dec 02 '15 at 15:08
  • The thing you need to know about finalize, is that you should *never* rely on it. – TT. Dec 02 '15 at 15:10
  • @TT. he is just learning how to use it, he isn't writing a real program here. – Ashwin Gupta Dec 02 '15 at 15:11
  • @AshwinGupta That is the one and only thing to learn about finalize. – TT. Dec 02 '15 at 15:13
  • Yes I'm reading Java- A beginner's guide @AshwinGupta – Dipanshu Juneja Dec 02 '15 at 15:14
  • why shouldn't I rely on it @TT ? I'm still not clear about when I might use it, I just read about it today – Dipanshu Juneja Dec 02 '15 at 15:17
  • I'll refer you to this [StackOverflow question/answer](https://stackoverflow.com/questions/2506488/when-is-the-finalize-method-called-in-java). – TT. Dec 02 '15 at 15:18
  • There is no guarantee whatsoever that finalize will ever be called. It might be called, it might not be called. Since there are no guarantees, you should never ever rely on it. It is a safeguard of some sort, but a real real bad one. – TT. Dec 02 '15 at 15:19
  • @DipanshuJuneja stick with that book, its great! It started me off on Java. Everything I know about has been from that book, online resources, or the javadoc. – Ashwin Gupta Dec 02 '15 at 15:20
  • Even if your program terminates and the object hasn't been finalized yet, even then there is no guarantee that finalize will be called. If your object allocated system resources, or resources on other servers eg, they might never be released. That may result in resources that are never released. In short, there is only one thing to know about finalize: never ever rely on it,. Instead use a [java.lang.AutoClosable along with the try-with-resources pattern](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html). – TT. Dec 02 '15 at 15:24

3 Answers3

0

Ok I'm going to break this up into parts.

How are we creating new objects using the same name 'o' in the for loop?

So, what you are doing here is actually just overriding/re-assiging the reference variable of o to a differnt FDemo. That leaves the old one empty and eventually gets destroyed by garbage collector. (Which is what invokes the finalize() method)

Would we have to create a new object and then on the next iteration discard the existing object 'o' throughout the loop?

Well that is exactly what you are doing, when you re assign o you are overwriting the old one.

Also, why does finalize execute for 10 million but not for 1000 if we are constantly creating and discarding the same object?

Well, this is because java's trash collector is only triggered when a massive amount of objects are made. However, I am surprised that 100000 didn't do it.

The thing you have to remember about finalize(), its only called on the trash collection of the object. Now, thats a very unreliable process and isn't really within your control. Like @TT. said in the comments, dont rely on this. The program that you have written simpliy forces the finalize() method to eventually be invoked due to generating a massive amount of objects intentionally. It's a good way to learn about finalize and how it works, but in practice this may not be a great solution.

Ashwin Gupta
  • 2,159
  • 9
  • 30
  • 60
0

Also, why does finalize execute for 10 million but not for 1000 if we are constantly creating and discarding the same object

You really create 10 million distinct objects in this applications's lifetime, each of which gets referenced by o , one after the other.

Each time, the de-referenced one becomes eligible for garbage collection, because it has become unreachable from the application, and the garbage collector then calls it's finalize method.

Arnaud
  • 17,229
  • 3
  • 31
  • 44
0

How are we creating new objects using the same name 'o' in the for loop?

You should learn some C and how pointers works, Java hide this, so it can be weird some time.

In Java, there is 2 type of data : objects (like String or your FDemo) and primitives (like int, char...).

Variables that references a primitive works like you intended to, each time you give it a new value, it erase the previous one. Variables that references objects don't work that way, they are pointers. A pointer can be seen as an address. It's clearer in C (and C++), where they are in fact primitives and can be print as well.

Next come the life time of your variables. When you quit the section where you variable is declared, it's cease to exist (but objects are not automatically destroyed and I think it's the purpose of your exercise). As example :

public void someMethod() {
    int a = 1;
    // I have access to a
    { // I create a new section, generally, it's use by condition, loop...
        int b = 2;
        // Here I have access to a and b
    } // End of my section
    // b is destroyed, I still have access to a
}
// I'm outside my method, a is destroyed

Why does finalize execute for 10 million but not for 1000 if we are constantly creating and discarding the same object?

That's the purpose of the Java's Garbage Collector. It's a big system used to clean memory. It destroys (call the finalize() method) all object that don't have pointers that references them. It's only called when you really need it, because it can use a lot of processing power (in old machine, applications could freeze when the garbage collector starts).

Your object is very simple (only 1 primitive), so it doesn't use a lot of memory. That's why the garbage collector is called only when you create 10 millions objects.

Erwan
  • 1
  • 1
  • I read about the Primitives vs object discussion in the book but your explanation was much more helpful than the text in the book. Thanks – Dipanshu Juneja Dec 02 '15 at 18:11