Java Final, Finalize & Final Class

  1. Craete final variables in Java
  2. final variable calculation in Java
  3. Create finalize method in Java
  4. Clear Garbage Collection of Objects in Java
  5. Create final class in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Craete final variables in Java


Final_Ex1.java

public class Final_Ex1 {
 
	public static final int a = 111;
	public static final float b = (float) 11.20;
	public static final double c = 1.123456789;
	public static final boolean d = true;
	public static final String e = "Final String Value";
 
	void display() {
		
		System.out.println("The Final Variables");
		
		System.out.println("\nInteger - " + a);
		System.out.println("Float - " + b);
		System.out.println("Double - " + c);
		System.out.println("Boolean - " + d);
		System.out.println("String - " + e);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Final_Ex1 obj = new Final_Ex1();
		obj.display();
	}
}

Sample Output

The Final Variables

Integer - 111
Float - 11.2
Double - 1.123456789
Boolean - true
String - Final String Value




B. final variable calculation in Java


Final_Ex2.java

import java.util.Scanner;
 
public class Final_Ex2 {
 
	public static final float PI = (float) 3.145;
	int r;
	Scanner scan = new Scanner(System.in);
 
	void getVal() {
		
		System.out.println("Area of Circle");
		System.out.println("\nEnter radious : ");
 
		r = scan.nextInt();
	}
	
	void display() {
		
		double area = PI * (r * r);
		
		System.out.println("\nArea : " + area);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Final_Ex2 obj = new Final_Ex2();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

Area of Circle

Enter radious : 
5

Area : 78.625




C. Create finalize method in Java


Finalize_Ex2.java

public class Finalize_Ex2 {
	
	public static void main(String[] args) throws Exception {
 
		Object f = new Object() {
			
			public void finalize() {
				
				System.out.println("Running finalize()");
			}
		};
 
		Runtime.getRuntime().addShutdownHook(new Thread() {
			
			public void run() {
				
				System.out.println("Running Shutdown Hook");
			}
		});
 
		if (args.length == 1 && args[0].equals("-f")) {
			
			f = null;
			System.gc();
		}
 
		System.out.println("Calling System.exit()");
		System.exit(0);
	}
}

Sample Output

Calling System.exit()
Running Shutdown Hook




D. Clear Garbage Collection of Objects in Java


Finalize_Ex1.java

public class Finalize_Ex1 {
	
	boolean check = false;
	
	Finalize_Ex1(boolean chk) {
		
		check = chk;
	}
	
	void ckeckIn() {
		
		check = false;
	}
	
	protected void finalize() {
		
		if(check)
			System.out.println("Error : Checked Out");
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Finalize_Ex1 obj = new Finalize_Ex1(true);
		
		obj.ckeckIn();
		
		new Finalize_Ex1(true);
		
		System.gc();
	}
}

Sample Output

Error : Checked Out




E. Create final class in Java


FinalClass_Ex1.java

public class FinalClass_Ex1 {
 
	protected int a = 10;
	protected int b = 20;
}
 
final class SubClass extends FinalClass_Ex1 {
		
	SubClass() {
		
		System.out.println("The final Class");
		System.out.println("This Class cannot be inherited..");
		System.out.println("It's the final class of inherited classes");
	}
	
	void add() {
		
		int c = a + b;
		System.out.println("\nThe Addition is : " + c);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		SubClass obj = new SubClass();
		obj.add();
	}
}

Sample Output

The final Class
This Class cannot be inherited..
It's the final class of inherited classes

The Addition is : 30






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com