Final
When Final is applied to a class: The class cannot be subclassed.
public final class FinalClassExample {...}When Final is applied to a method: The method cannot be overridden.
public class MyClass {
public final void FinalMethodExample() {...}
}When Final is applied to a variable primitive or reference: The value of the variable cannot change or cannot point to any other object on the heap.
public class FinalVariableExample {
public static void main(String[] args) {
final int minutesInHour=60;
}
}Finally
“Finally” is an optional block called after “try” block or after “catch” block. Statements in the finally block will always be executed (except when System.exit() is called in "try" or "catch" blocks).
try {
// regualr execution path
} catch (SampleException ee) {
// handle SampleException
} finally {
// This optional section is executed upon termination of any of the try or catch blocks above,
// except if JVM exits from the try or catch blocks
}
Finalize()
This is the method that the JVM runs before running the garbage collector. Before an object is garbage collected, the runtime system calls its finalize() method.
protected void finalize() throws Throwable {
. . .
// clean up code for here
. . .
super.finalize();
}
This is must know Java interview question after two years of software development experience with Java.
0 comments:
Post a Comment