Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize access specifiers protected/private/public

 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because finalize is declared protected in java.lang.Object and Java doesn't allow you to reduce visibility in subclasses.
 
Siva kandasamy
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sense. Thanks.
-siva
[ January 03, 2003: Message edited by: Sivanantham kandan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic