Thursday 23 June 2016

Java Tutorial : Java Exception handling (final vs finally vs finalize)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=9JDbBNs6dhc&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Exception handling (final vs finally vs finalize) 
FinalDemo1.java
public class FinalDemo1
{
    public static void main(String[] args)
    {
        /*
         * If the final keyword is attached to a variable
         * then the variable becomes constant i.e. its value
         * cannot be changed in the program.
         */
        final int intValue = 1000;
        intValue = 500;// Compile Time Error
    }

}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The final local variable intValue cannot be assigned. It must be blank and not using a compound assignment

    at FinalDemo1.main(FinalDemo1.java:11)
FinalDemo2.java
class SuperClass
{
    final void methodOfSuperClass()
    {
        System.out.println("final Method");
    }
}
 
class SubClass extends SuperClass
{
    void methodOfSuperClass()
    {
        //Compile time error, final method can not be overridden.
    }
}

public class FinalDemo2
{
    public static void main(String[] args)
    {
        SuperClass superClass = new SubClass();
        superClass.methodOfSuperClass();
    }

}
Output
Exception in thread "main" java.lang.VerifyError: class SubClass overrides final method methodOfSuperClass.()V
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
FinalDemo3.java
/*
 * If a class is marked as final then this class 
 * cannot be inherited by any other class.
 */

final class SuperClass1
{
    void methodOfSuperClass()
    {
        System.out.println("final class");
    }
}

class SubClass1 extends SuperClass1
{
    
}

public class FinalDemo3
{
    public static void main(String[] args)
    {
        SuperClass1 superClass1 = new SubClass1();
        superClass1.methodOfSuperClass();
    }

}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Type mismatch: cannot convert from SubClass1 to SuperClass1

    at FinalDemo3.main(FinalDemo3.java:23)
FinalizeDemo.java
public class FinalizeDemo
{

    private String name = null;

    public FinalizeDemo(String name)
    {
        super();
        this.name = name;
    }

    public static void main(String[] args)
            throws InterruptedException
    {
        FinalizeDemo FinalizeDemo1 = new FinalizeDemo("FinalizeDemo1");
        FinalizeDemo1 = null;
        FinalizeDemo FinalizeDemo2 = new FinalizeDemo("FinalizeDemo2");
        FinalizeDemo1 = null;
        FinalizeDemo2 = null;
        System.gc();
        Thread.sleep(100);
    }

    public void finalize()
    {
        System.out.println(this.name
                + " Object finalize method is called");
    }

}
Output
FinalizeDemo2 Object finalize method is called
FinalizeDemo1 Object finalize method is called
FinallyDemo.java
public class FinallyDemo
{

    public static void main(String[] args)
    {
        try
        {
            String str = "Peter";
            System.out.println("str = "+str);
        }
        catch (Exception exe)
        {
            exe.printStackTrace();
        }
        finally
        {
            System.out.println("finally block is executed");
        }
    }

}
Output
str = Peter
finally block is executed
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ExceptionDemo_final_finally_finalize_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ExceptionDemo_final_finally_finalize_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/969ffd88eb34c4fd42d966118b27490392b78865/BasicJava/ExceptionDemo_final_finally_finalize_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • 2 comments:

    1. Excellent…Amazing…. I’m satisfied to find so many helpful information here within the put up,for latest php jobs in near me. we want work out extra strategies in this regard, thanks for sharing.

      ReplyDelete