Before starting your job search, look at these common Java interview questions asked during real interviews and understand the answers, but instead of memorizing the answers try to complete further research and come with your own personal solution for the interview question. The more prepared you are for what you will encounter, the more successful you will be. This collection of Java interview question could be also useful for software developers who need to interview Java developer.

Java Interview Question 7: What is final finally finalize difference?

Java interview question about final, finally, and finalize is a straightforward question and software developer with an experience should not blabber on about a problem, but should stay light on details and just state the key points.

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.

No comments: