Please explain what purpose does the keywords final, finally, and finalize fulfill?
Submitted by: MuhammadFinal:
Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed. Let's take a look at the example below to understand it better.
class FinalVarExample {
public static void main( String args[])
{
final int a=10; // Final variable
a=50; //Error as value can't be changed
}
Finally
Finally is used to place important code, it will be executed whether exception is handled or not. Let's take a look at the example below to understand it better.
class FinallyExample {
public static void main(String args[]){
try {
int x=100;
}
catch(Exception e) {
System.out.println(e);
}
finally {
System.out.println("finally block is executing");}
}}
}
Finalize
Finalize is used to perform clean up processing just before object is garbage collected. Let's take a look at the example below to understand it better.
class FinalizeExample {
public void finalize() {
System.out.println("Finalize is called");
}
public static void main(String args[])
{
FinalizeExample f1=new FinalizeExample();
FinalizeExample f2=new FinalizeExample();
f1= NULL;
f2=NULL;
System.gc();
}
}
Submitted by: Muhammad
Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed. Let's take a look at the example below to understand it better.
class FinalVarExample {
public static void main( String args[])
{
final int a=10; // Final variable
a=50; //Error as value can't be changed
}
Finally
Finally is used to place important code, it will be executed whether exception is handled or not. Let's take a look at the example below to understand it better.
class FinallyExample {
public static void main(String args[]){
try {
int x=100;
}
catch(Exception e) {
System.out.println(e);
}
finally {
System.out.println("finally block is executing");}
}}
}
Finalize
Finalize is used to perform clean up processing just before object is garbage collected. Let's take a look at the example below to understand it better.
class FinalizeExample {
public void finalize() {
System.out.println("Finalize is called");
}
public static void main(String args[])
{
FinalizeExample f1=new FinalizeExample();
FinalizeExample f2=new FinalizeExample();
f1= NULL;
f2=NULL;
System.gc();
}
}
Submitted by: Muhammad
Read Online Sr.Java Web Developer Job Interview Questions And Answers
Top Sr.Java Web Developer Questions
☺ | Do you know what is composition in Java? |
☺ | Please explain what is the difference between execute, executeQuery, executeUpdate? |
☺ | Tell us what is Hibernate Framework? |
☺ | Tell me what are the different tags provided in JSTL? |
☺ | Tell us what is difference between Error and Exception? |
Top Java Programming Language Categories
☺ | Core Java Interview Questions. |
☺ | Hibernate Interview Questions. |
☺ | Advanced Java Interview Questions. |
☺ | IBM WebSphere Interview Questions. |
☺ | Spring Framework Interview Questions. |