| Author |
finalize access specifiers protected/private/public
|
Siva kandasamy
Ranch Hand
Joined: Dec 31, 2002
Posts: 139
|
|
Hi There, Refer the example #1 "Finalize.java" below. Change the finalize method with following piece of code ie. void finalize(){ System.out.println("finalize-TestA"); } OR private void finalize(){ System.out.println("finalize-TestA"); } The question is, If we do that, we get compilation error. Please help me to understand, why do I get compilation error; if I use access specified other than public or protected. Hope my question make sense. Thanks in Advance Siva *********** Example #1 ************************** // Finalize.java class TestA { TestA(){ System.out.println("TestA"); } void Hello(){ System.out.println("Hello-TestA"); } protected void finalize(){ System.out.println("finalize-TestA"); } } class Finalize { Finalize(){ System.out.println("Finalize"); } public static void main(){ System.out.println("finalize-TestA"); } } ******************************************** [ January 02, 2003: Message edited by: Sivanantham kandan ]
|
 |
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
|
|
|
If the method is private then only the class itself can call it. This is a reserved method that is ONLY to be called by the gc. For the gc to get AT it, it needs to have an appropriate accessibility.
|
"JavaRanch, where the deer and the Certified play" - David O'Meara
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
Because finalize is declared protected in java.lang.Object and Java doesn't allow you to reduce visibility in subclasses.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Siva kandasamy
Ranch Hand
Joined: Dec 31, 2002
Posts: 139
|
|
Make sense. Thanks. -siva [ January 03, 2003: Message edited by: Sivanantham kandan ]
|
 |
 |
|
|
subject: finalize access specifiers protected/private/public
|
|
|