Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   finalize() (http://www.velocityreviews.com/forums/t390743-finalize.html)

cy 02-09-2007 10:04 PM

finalize()
 
Is there a way to make sure that finalization always occurs; that an
object's finalize() method is always called before program exit?
Thanks


Eric Sosman 02-09-2007 10:17 PM

Re: finalize()
 
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.

--
Eric.Sosman@sun.com

grasp06110@yahoo.com 02-09-2007 11:05 PM

Re: finalize()
 
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");
}
}


cy 02-10-2007 02:12 AM

Re: finalize()
 
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?
-----------


cy 02-10-2007 02:15 AM

Re: finalize()
 
On Feb 9, 5:17 pm, Eric Sosman <Eric.Sos...@sun.com> 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.
>
> --
> Eric.Sos...@sun.com


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


Lew 02-10-2007 02:40 AM

Re: finalize()
 
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

Lew 02-10-2007 02:43 AM

Re: finalize()
 
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

Chris Uppal 02-10-2007 04:15 PM

Re: finalize()
 
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



nukleus 02-10-2007 05:22 PM

Re: finalize()
 
In article <45cdf06d$0$756$bed64819@news.gradwell.net>, "Chris Uppal"
<chris.uppal@metagnostic.REMOVE-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
>
>


Lew 02-11-2007 05:45 AM

Re: finalize()
 
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


All times are GMT. The time now is 01:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.