Used to declare constants, prevent method overriding, or stop class inheritance.
---------------------------------------------------------------------------------------------------------------------------------------------
final int x = 10; // x cannot be reassigned
final class Vehicle {
// This class cannot be extended
}
class Car {
final void start() {
System.out.println("Engine starts");
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
Used in exception handling to execute code regardless of whether an exception occurs.
---------------------------------------------------------------------------------------------------------------------------------------
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero");
} finally {
System.out.println("Cleanup code runs always");
}
---------------------------------------------------------------------------------------------------------------------------------------------
A method that runs before an object is garbage collected. Deprecated in modern Java.
-----------------------------------------------------------------------------------------------------------------------------------
public class FinalizeDemo {
@Override
protected void finalize() throws Throwable {
System.out.println("Object is being garbage collected");
}
public static void main(String[] args) {
FinalizeDemo obj = new FinalizeDemo();
obj = null;
System.gc(); // Suggest garbage collection
}
}
============================================================
java,final,finally,finalize,java,keywords,java,interview,questions,java,exceptions,java,garbage,collection,java,programming,object,oriented,concepts,java,developer,java,learning,java,tutorial,java,for,beginners,java,core,concepts,java,code,examples
Информация по комментариям в разработке