answersLogoWhite

0

What is finalize method in java?

Updated: 10/19/2022
User Avatar

Wiki User

11y ago

Best Answer

Finalize method is used when a variable becomes unreachable or of no use.

it is finalize and garbage collection will free the memory used by it, which can be then reclaimed for some other variable

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

The final keyword prevents a method from being overridden in a subclass. Final methods are usually placed in classes where you don't want anyone who is extending your code to meddle with a certain piece of functionality. For ex: the Thread class has a method called isAlive() that checks whether a thread is still active. If you extend the Thread class, there is no way that you can correctly implement this method yourself, so the men who coded Java have made it final. Just like you can't subclass the String class (remember the Exercise in the final classes section), you can't override many of the methods in the core class libraries. This can't-be-overridden restriction provides for safety and security, but you should use it with great caution. Preventing a subclass from overriding a method greatly affects many of the benefits of OO including extensibility through polymorphism.

A typical final method declaration looks like this:

public class FinalMethodExampleClass{

public final void youCannotOverrideMe() {

System.out.println("One.");

}

}

It's legal to extend FinalMethodExampleClass, since the class isn't marked final, but we can't override the final method youCannotOverrideMe (), as the following code attempts to do:

class SubClass extends FinalMethodExampleClass {

public void youCannotOverrideMe () {

System.out.println("Two.");

}

}

Attempting to compile the preceding code gives us something like this:

The method void youCannotOverrideMe() declared in class

SubClass cannot override the final method of the same signature

declared in class SuperClass.

Final methods cannot be overridden.

public void youCannotOverrideMe() { }

1 error

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is finalize method in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How does java support the concept of destructer in java?

In Java, Java does support the concept of destructor, it's done via special method finalize.


What do you use instead of a destructor in java?

Hi, This is suneetha. We use Finalize() method in java instead of destructors. Finalize() is used to release the memory before the garabage collection takes place.


What is the difference between garbage collector and finalization?

When all references to an object is Java are gone, the garbage collector will come along and free up the memory used by that object.Every Java object has a method named finalize, which is called by the garbage collector when the memory for that object is being deallocated. "Finalization" is just a name given to the act of calling the finalize method during garbage collection.


What are destructor in java?

Destructors in Java are called finalizers. Every class can define a finalize() method that will get called automatically by the garbage-collector when an instance of the class gets garbage-collected. Finalizers are not guaranteed to get called, as the instance might never get collected.


Why java does not support destructor?

AMIT KUMAR3th Nov , 2014Java does not support destructors, since java supports the concept of garbage collection,hence the garbage collector is used to automatically free the space which has occupied by the program while running.Garbage collector calls the finalize() method which is defined in the object class. finalize() method is called once for each object.


What is overridnig method and overlording method in java?

There is no such thing as overlording in Java.


What is the task of the main method in java platform?

It is the method that gets called when a Java application is started.


How nesting of methods is done in java?

No, Java only allows a method to be defined within a class, not within another method.


What is function in java terminology?

In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.In Java, a function is called a "method". In Java as well as other languages, a method is a function defined specifically for one class. In Java, this is the only way to define functions, therefore, all functions are methods.


When do you override the finalize method?

when there is cleanup activity needed before an object is destroyed


What is a method in Java?

A Java method is a sequence of statements. It is comparable to a function, subroutine, or procedure in other languages.


What is the purpose of NoMatchException handling method in java?

java exception