1

I'm porting to Java some C++ code that uses the pretty common C++ trick of allocating an object on the stack (it happens to implement a UDP connection) which has some internal state information (here, a UDP socket). While in scope the object is used to do various things (send and receive UDP messages). The nice thing is that when control leaves this scope the object's destructor will be run and this can be counted upon to automagically cause the release the object's internal resources (in this case, I make sure the socket gets closed so that I can reuse its address and port numbers in other parts of my program).

In trying to figure out how to do this I have learned that Java does not have a destructor, that "finalize" won't do what I want, etc.

Surely there is some similarly clever technique of accomplishing the same thing in Java? I realize I could add a "close()" method and try to make sure that it is always called at the appropriate time, and I further realize this would probably be more easily and reliably done in Java than in C++. But do I really have to go that route?

Daniel Chisholm
  • 566
  • 5
  • 16
  • Yes if I have to end up doing it manually I suspect `try{} finally {}` will be a clean way to do it. I'm a bit surprised though that it looks like I probably will be forced to manually code this everywhere I need it done. – Daniel Chisholm Jul 08 '11 at 19:10

4 Answers4

2

I believe Java 7 supports Automatic Resource Block Management, which may be what you're looking for.

aardvarkk
  • 14,955
  • 7
  • 67
  • 96
  • Yes that is the functionality I am looking for. I am a bit surprised that this suggests that today, I'm going to have to code this up manually? – Daniel Chisholm Jul 08 '11 at 19:13
1

Have a finally{} block in your code that closes the connection and nulls it. This will make it eligible for garbage collection. Thats all you can do.

Kal
  • 24,724
  • 7
  • 65
  • 65
0

Java will automatically clean up everythibg with out a reference, as well as the so called "islands", this is done with automatic garbage collection. You can suggest the JVM to collect at any tine but there is no guarantee that it will comply. The only guatantee is that before the JVM runs out of memory it will try to clean everything it can.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
-1

A simple search right here on SO would have revealed a multitude of questions and answers pertaining to the same topic. I took the liberty of copying some of the juicier ones and posting them here.

Is there C++ destructor equivalent in Java?

Is there a destructor for Java?

Why does Java not have any destructor like C++?

What is the best way to clean up an Object in Java?

How do you write a deconstructor in Java?

I'm a walking search engine.

Community
  • 1
  • 1
Perception
  • 79,279
  • 19
  • 185
  • 195
  • Thanks; I'd already found and read the first four of those and while they gave me some useful background info on java they were not quite what I needed. – Daniel Chisholm Jul 08 '11 at 19:00