Why do I need to call the close () or shutdown () method?

I am new to Java with some experience with C ++ at my school. Now I'm trying to do something, and I chose Java as a programming language.

I did my homework and will talk a lot about the "destructors" for the Java methods, finalize () and close () or shutdown (). But still, I think I have no idea how this should work (more on this below)

OK, a specific question would be the reason why I need to call the close () or shutdown () methods?

In my particular case, I am working with a class that I did not develop that handles a smart card reader, but I saw that the file management case where you should call the close () method would be similar.

Does the close () method not call the same idea as freeing memory in C ++ (which sucks)? Sense, I have to take care of deleting or destroying an object ... not a GC for?

This may be an option that the class I'm trying to use for a smart card reader is not the best, maybe it is better for this class to implement the finalize () method, so when it is no longer used and not ready to use the GC, frees memory (most likely it's native code) and / or frees up hardware resources that the GC may not know how to do.

But what about file management classes? They are very used and supported, why is the close () method still needed? I understand the purpose of existence to unlock a file, but why should I remember closing it? Once the object is no longer in use, unlock the file automatically, at least in the most common cases.

Finally, is there a suitable workaround to wrap a class that needs to be closed or completed with a class that implements the finalize () method, and there I call the close () or shutdown () method?

I saw that the finalize () method is not very popular, so I ask how this problem should be solved.

Thanks in advance

Juan

PS: what i saw:

Is there a destructor for Java?

Why would you ever implement finalize ()?

http://www.codeguru.com/java/tij/tij0051.shtml

close() Java Layman

, File.getName(), ?

+5
4

finalize, ++ - , , , , , ( , ). , , , (, , ); , , , , , . System.gc , , , , , , , .

, close shutdown, , / , , .

, try-catch-finally, , close shutdown - finally. , Java7, try-with-resources.

+3

java , ( ) close (, ..),.

, close() , , :

conn.close();
conn = null;

, , .

+2

Java , .

. , .

, , .close .shutdown?

, .

, , , ?

Java 7 try-with-resources, .

, , , finalize, close shutdown?

. finalize(), , .

+1

. , - , . , , , .

close() , ; , ( ) , , . .

, , , , , . , close. , , , - . , , , , . , , , , .

+1

All Articles