This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Java in General and the fly likes finalize method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "finalize method" Watch "finalize method" New topic
Author

finalize method

Vivek Nanda
Greenhorn

Joined: Jun 04, 2004
Posts: 9
Hi..i seem to be doing something wrong but cant get the reason.

The following piece of Code explains:
----------------------------------------------------------------
class Testing {

public static void main(String[] args) {
Object obj = new Object();
obj.finalize();
}
}

--------------------------------------------------------------------

When i try to compile i get the following error:

error :finalize() has protected access in java.lang.Object

--------------------------------------------------------------------

When i do the same with the following 2 classes it works fine.

class Mall
{
protected void getName(){
System.out.println("The Protected Method is called.");
}

}

------

class Testing
{
public static void main(String[] args)
{
Mall m = new Mall();
m.getName();
}
}
----------------------------------------------------------

Can Anyone please explain ...or point out where the my mistake is

Thanks...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

protected methods are available to subclasses and to other classes in the same package. Mall and Testing are in the same package.


[Jess in Action][AskingGoodQuestions]
Lalit Kapoor
Greenhorn

Joined: Jun 06, 2004
Posts: 20
Hello Ernest
Because protected methods are available to subclasses and finalize() method is available in the Object class, then there should be no compile time error because every class extends Object class by default. Then why is the finalize() method not accessible?
Also can we call finalize() method explicitly, because i think only gc() can call finalize() method. What do u think?
Ramakrishnan Ponmudi
Ranch Hand

Joined: Apr 20, 2004
Posts: 72
hai ,
analyze my answer & send reply to me

Object contains no instance(member) variables,
so if u create object for Object ,no object is created in heap.
just u can use methods in sub classes.
--no need to call garbage colletor to collect

note : runtime object contains only member variables, not member functions.

so, u will not allowed to call finalize method.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Thanjai ramakrishnan:

Object contains no instance(member) variables,
so if u create object for Object ,no object is created in heap.


Yery creative, but no.

Object o = new Object();

gives rise to a perfectly lovely, quite real object on the heap.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Lalit Kapoor:
Hello Ernest
Because protected methods are available to subclasses and finalize() method is available in the Object class, then there should be no compile time error because every class extends Object class by default. Then why is the finalize() method not accessible?


The rule is actually kind of subtle. Outside of the package that defines it, a protected member can be accessed only through an object reference from the same "inheritance tree" as the calling code. For example, if a protected member x() is defined in class A, and A has two subclass B and C, then code in B can call x() only on objects of type B, code in C can call x() only on C objects, and code in A can call x() on A, B, and C objects. If B has a subclass D, then code in B could also call x() on a D.


Also can we call finalize() method explicitly, because i think only gc() can call finalize() method. What do u think?


finalize() is just a plain old ordinary method like any other. You can indeed call it. Note that the default implementation doesn't do anything, though -- it's just an empty method. The purpose of finalize() is to give you a place to put code the GC should call before destroying an object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finalize method