This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finalize access specifiers public/protected/private Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "finalize access specifiers public/protected/private" Watch "finalize access specifiers public/protected/private" New topic
Author

finalize access specifiers public/protected/private

Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi There,
Refer the example #1 "Finalize.java" 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 you do change, you will get compilation error.
Please help me to understand, why do we get compilation error, if we use access specified other than public or protected.
Hope my posting 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");
}
}
********************************************
Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
Hi Sivanantham ,
"protected void finalize()" is a method in Object class, which is a superclass of every class.
The access specifier in a method that overrides a method in the super class should be same or more than than access in the method in the super class. So the finalize() method in your class can be protected or public. It can't be package or private.
cheers,
deep
 
Consider Paul's rocket mass heater.
 
subject: finalize access specifiers public/protected/private