Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > finalize()

Reply
Thread Tools

finalize()

 
 
cy
Guest
Posts: n/a
 
      02-09-2007
Is there a way to make sure that finalization always occurs; that an
object's finalize() method is always called before program exit?
Thanks

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-09-2007
cy wrote On 02/09/07 17:04,:
> Is there a way to make sure that finalization always occurs; that an
> object's finalize() method is always called before program exit?


There are ways to improve the chances the finalization
will occur, but as far as I know there is no way to be
completely sure.

Why do you care? Are you a C++ convert who thinks
finalizers are destructors? They're not, and can't act
as destructor substitutes -- not reliably, anyhow.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
grasp06110@yahoo.com
Guest
Posts: n/a
 
      02-09-2007
I believe the spec states that there is no guarentee that finalize
will ever be called. Also, there is certianly no guarentee as to
*when* finalize will be called.

A fairly effective way to ensure resources are deallocated is to write
a super class that alocates the resource and subclasses to use the
resource. Something like the following:

public abstract class DoSomething {
public abstract void doIt();
public void doSomething() throws Exception {
File file = null;
try {
this.doIt();
} finally {
if(file != null) {
file.close();
}
}
}
}


public class Doer extends DoSomething {
public void doIt() throws Exception {
throw new Exception("i hope this doesn't cause any problems");
}
}

 
Reply With Quote
 
cy
Guest
Posts: n/a
 
      02-10-2007
thanks, grasp
so the compiler (javac) produces one error when given the following:
class File {
void close(){}
}
abstract class DoSomething {
public abstract void doIt();
public void doSomething() throws Exception {
File file = null;
try {
this.doIt();
} finally {
if(file != null) {file.close();}
}
}

}

public class Doer extends DoSomething {
public void doIt() {};
public void doSomething() throws Exception {
throw new Exception("i hope this doesn't cause any problems");
}
public static void main(String[] args) {
Doer d = new Doer();
d.doSomething();
System.gc();
}
}
----------------
error: line 26: unreported exception java.lang.Exception; must be
caught of declared to be thrown: d.soSomething():
---------------
make sense?
-----------

 
Reply With Quote
 
cy
Guest
Posts: n/a
 
      02-10-2007
On Feb 9, 5:17 pm, Eric Sosman <(E-Mail Removed)> wrote:
> cy wrote On 02/09/07 17:04,:
>
> > Is there a way to make sure that finalization always occurs; that an
> > object's finalize() method is always called before program exit?

>
> There are ways to improve the chances the finalization
> will occur, but as far as I know there is no way to be
> completely sure.
>
> Why do you care? Are you a C++ convert who thinks
> finalizers are destructors? They're not, and can't act
> as destructor substitutes -- not reliably, anyhow.
>
> --
> (E-Mail Removed)


thanks, Eric
yes, I learned C++ first 'cause was told would facilitate learning
java;
appreciate difference between java finalize() and C++ destructor, just
wondering if finalization can be essentially forced -- apparently not.
Greg

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-10-2007
cy wrote:
> class File {
> void close(){}
> }
> abstract class DoSomething {
> public abstract void doIt();
> public void doSomething() throws Exception {
> File file = null;
> try {
> this.doIt();
> } finally {
> if(file != null) {file.close();}
> }
> }
>
> }
>
> public class Doer extends DoSomething {
> public void doIt() {};


You failed to implement the abstract method.

> public void doSomething() throws Exception {
> throw new Exception("i hope this doesn't cause any problems");
> }


The idea is not to override doSomething() but to let the superclass
doSomething() call the overridden doIt().

> public static void main(String[] args) {
> Doer d = new Doer();
> d.doSomething();


This must be line 26. (You might have considered helping on this matter.)

You failed to catch the Exception that doSomething() might throw.

> System.gc();


This is a dilatory call. It is not guaranteed to run the gc, and if it does
might cause a full generational collection instead of a simple nursery one,
thus possibly reducing performance.

> }
> }
> ----------------
> error: line 26: unreported exception java.lang.Exception; must be
> caught of declared to be thrown: d.soSomething():


The call to d.doSomething() (notice where the message points that out?) might
throw an Exception, which "must be caught *or* declared" [emphasis added - you
misspelled the message - next time copy and paste it to avoid typos], just
like the message says. Either put a try ... catch around the call or declare
the method that uses it to rethrow the Exception. (Which makes no sense to do
with main().)

> ---------------
> make sense?
> -----------


Perfect sense.

- Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-10-2007
cy wrote:
>> public class Doer extends DoSomething {
>> public void doIt() {};


Lew wrote:
> You failed to implement the abstract method.


My mistake, I mean there is a syntax error in the implementation. There should
not be a semicolon there.

- Lew
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-10-2007
cy wrote:

> yes, I learned C++ first 'cause was told would facilitate learning
> java;


Whoever told you that was an idiot^H^H^H^H^H^H^H^Hwrong.

-- chris


 
Reply With Quote
 
nukleus
Guest
Posts: n/a
 
      02-10-2007
In article <45cdf06d$0$756$(E-Mail Removed)>, "Chris Uppal"
<(E-Mail Removed)-THIS.org> wrote:
>cy wrote:
>
>> yes, I learned C++ first 'cause was told would facilitate learning
>> java;

>
>Whoever told you that was an idiot^H^H^H^H^H^H^H^Hwrong.


Huh?
What is java on the first place, mr. mouth foaming smart?
Can you describe it?

Before you do that, i can just tell you,
it is about 90% C code and the object oriented concepts
come from C++ ideas that were ALREADY implemented
and verified to be viable.

I did read some rants by one of the highest priests,
architects at Sun, but his arguments as to the very
language seemed to be nothing more than mouth foaming,
claiming that C++ is completely wacky and incomprehencible.
This dude, who is probably the CHIEF architect at Sun
is simply a lunatic.

Simple as that.

Zo...

Kindly describe what constitutes java as such
and what makes it fundamentally different.

Sure, some things like thread concepts and other
operating system level ideas are expressed on a language
level, which I do support because it makes it more portable.
But that is just a beginning of the story and not the end.

> -- chris
>
>

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-11-2007
cy wrote:
>>> yes, I learned C++ first 'cause was told would facilitate learning
>>> java;


"Chris Uppal" wrote:
>> Whoever told you that was an idiot^H^H^H^H^H^H^H^Hwrong.


nukleus wrote:
> Huh?
> What is java on the first place, mr. mouth foaming smart?


The /ad hominem/ attack does not go far to support your point.

> Can you describe it?
>
> Before you do that, i can just tell you,
> it is about 90% C code and the object oriented concepts
> come from C++ ideas that were ALREADY implemented
> and verified to be viable.


Aside from the arbitrary nature of that "90%", it is the similarities of C++
to Java that contribute to the difficulty of making the switch. For example,
C++ "reference" semantics and Java "reference" semantics are not the same,
which can mess up anyone moving back and forth between them until they get
used to the differences. It is the subtlety of the differences that makes them
difficult.

> I did read some rants by one of the highest priests,
> architects at Sun, but his arguments as to the very
> language seemed to be nothing more than mouth foaming,


The word "seemed" in its passive voice disguises the semantic of making a
judgement while obfuscating the lack of evidence for it.

> claiming that C++ is completely wacky and incomprehencible.
> This dude, who is probably the CHIEF architect at Sun
> is simply a lunatic.
>
> Simple as that.
>
> Zo...
>
> Kindly describe what constitutes java [sic] as such
> and what makes it fundamentally different.


What constitutes Java as such is the Java Language Specification:
<http://java.sun.com/docs/books/jls/index.html>
.. There are many things that "make... it fundamentally different" from C++ -
built-in Hoare monitors, GC, lack of templates, no preprocessor, different
reference semantics, lack of pointer arithmetic, pragmatic culture vs.
ivory-tower culture, little variegated parrots that peck at suet, ...

> Sure, some things like thread concepts and other
> operating system level ideas are expressed on a language
> level, which I do support because it makes it more portable.
> But that is just a beginning of the story and not the end.


You are exactly right.

- Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments