Everson,
Vamos l�:
1) O m�todo finalize() � executado no momento em que o Garbage Colector
tenta liberar a mem�ria de um objeto que n�o mais est� sendo utilizado.
Antes de liberar a mem�ria o m�todo finalize associado a esse objeto �
executado, e somente depois o GC libera a mem�ria. A� vai um exemplo...
public class GCTest {
public static void main(String[] args) {
Blah a = new Blah();
Blah b = new Blah();
Blah c = new Blah();
a.i = 1;
b.i = 2;
c.i = 3;
b = a; // O objeto 'b' passa a apontar para
// a mesma refer�ncia do objeto 'a'
System.out.println(
"a:" + a.i + ", b: " + b.i + ", c: " + c.i);
System.gc(); // Sugere a execu��o do GC
}
}
class Blah {
int i;
protected void finalize() {
System.out.println(
"Valor de 'i' para o objeto coletado pelo GC: "
+ this.i);
}
}
2) Maior clareza do c�digo.
3) Retorna uma refer�ncia ao objeto atual.
Espero ter ajudado.
[]'s
Ruy
Ruy Pedroso
Trails Sistemas
Av. Nilo Pe�anha 50 - sala 2511 - Centro
CEP 20020-100 - Rio de Janeiro - RJ
Tel: +55 21 2262 6370
Fax: +55 21 2262 6354
-----Original Message-----
From: everson [mailto:[EMAIL PROTECTED]]
Sent: sexta-feira, 24 de janeiro de 2003 14:17
To: [EMAIL PROTECTED]
Subject: [java-list] duvidas -> finalize() + this
1) alguem pode me explicar como funciona o m�tode finalize() ? Algum
exemplo
!?
**********
2) Nas situacoes em q this � opcional, qual a vantagem no seu uso ????
3)Alguem pode me explicar oq this retorna nos 2 metodos abaixo:
public Time4 setHour( int h )
{
this.hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
return this; // enables chaining
}
public Time4 setTime( int h, int m, int s )
{
this.setHour( h ); // set the hour
this.setMinute( m ); // set the minute
this.setSecond( s ); // set the second
return this; // enables chaining
}
************************************************************************
****
***************************
import java.text.DecimalFormat; // used for number formatting
public class Time4 extends Object {
private int hour; // 0 - 23
private int minute; // 0 - 59
private int second; // 0 - 59
public Time4() { this.setTime( 0, 0, 0 ); }
public Time4( int h ) { this.setTime( h, 0, 0 ); }
public Time4( int h, int m ) { this.setTime( h, m, 0 ); }
public Time4( int h, int m, int s )
{ this.setTime( h, m, s ); }
public Time4( Time4 time )
{
this.setTime( time.getHour(),
time.getMinute(),
time.getSecond() );
}
public Time4 setTime( int h, int m, int s )
{
this.setHour( h ); // set the hour
this.setMinute( m ); // set the minute
this.setSecond( s ); // set the second
return this; // enables chaining
}
public Time4 setHour( int h )
{
this.hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
return this; // enables chaining
}
public Time4 setMinute( int m )
{
this.minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
return this; // enables chaining
}
public Time4 setSecond( int s )
{
this.second = ( ( s >= 0 && s < 60 ) ? s : 0 );
return this; // enables chaining
}
public int getHour() { return this.hour; }
public int getMinute() { return this.minute; }
public int getSecond() { return this.second; }
public String toUniversalString()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );
return twoDigits.format( this.getHour() ) + ":" +
twoDigits.format( this.getMinute() ) + ":" +
twoDigits.format( this.getSecond() );
}
public String toString()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );
return ( ( this.getHour() == 12 ||
this.getHour() == 0 ) ?
12 : this.getHour() % 12 ) + ":" +
twoDigits.format( this.getMinute() ) + ":" +
twoDigits.format( this.getSecond() ) +
( this.getHour() < 12 ? " AM" : " PM" );
}
}
------------------------------ LISTA SOUJAVA
----------------------------
http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-SP
d�vidas mais comuns: http://www.soujava.org.br/faq.htm
regras da lista: http://www.soujava.org.br/regras.htm
historico: http://www.mail-archive.com/java-list%40soujava.org.br
para sair da lista: envie email para
[EMAIL PROTECTED]
------------------------------------------------------------------------
-
---
Incoming mail is certified Virus Free.O email foi inspecionado por
v�rus.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 27/1/2003
---
Outgoing mail is certified Virus Free.O email foi inspecionado por
v�rus.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.449 / Virus Database: 251 - Release Date: 27/1/2003
------------------------------ LISTA SOUJAVA ----------------------------
http://www.soujava.org.br - Sociedade de Usu�rios Java da Sucesu-SP
d�vidas mais comuns: http://www.soujava.org.br/faq.htm
regras da lista: http://www.soujava.org.br/regras.htm
historico: http://www.mail-archive.com/java-list%40soujava.org.br
para sair da lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------