Kuncle

God I pray to prosper thee.

Java Foundation FAQ

What is the difference between NoClassDefFoundError and ClassNotFoundException?

  • NoClassDefFoundError

        Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found. The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

  • ClassNotFoundException

        Thrown when an application tries to load in a class through its string name using:
            The forName method in class Class.
            The findSystemClass method in class ClassLoader.
            The loadClassmethod in class ClassLoader.
    but no definition for the class with the specified name could be found.
        As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The “optional exception that was raised while loading the class” that may be provided at construction time and accessed via the getException() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned “legacy method.”

What is the difference between final, finally and finalize?

  • final

        final is a reserved keyword in java. We can’t use it as an identifier as it is reserved. We can use this keyword with variables, methods and also with classes. The final keyword in java has different meaning depending upon it is applied to variable, class or method.

  • final with Variables

        The value of variable cannot be changed once initialized. If we declare any variable as final, we can’t modify its contents since it is final, and if we modify it then we get Compile Time Error.

  • final with Class

        The class cannot be subclassed. Whenever we declare any class as final, it means that we can’t extend that class or that class can’t be extended or we can’t make subclass of that class.

  • final with Method

        The method cannot be overridden by a subclass. Whenever we declare any method as final, then it means that we can’t override that method.

  • finally

        The finally keyword is used in association with a try/catch block and guarantees that a section of code will be executed, even if an exception is thrown. The finally block will be executed after the try and catch blocks, but before control transfers back to its origin.

  • finalize

        It is a method that the Garbage Collector always calls just before the deletion/destroying the object which is eligible for Garbage Collection, so as to perform clean-up activity. Clean-up activity means closing the resources associated with that object like Database Connection, Network Connection or we can say resource de-allocation. Remember it is not a reserved keyword.
        Once finalize method completes immediately Garbage Collector destroy that object. finalize method is present in Object class and its syntax is:
            protected void finalize throws Throwable{}
        Since Object class contains finalize method hence finalize method is available for every java class since Object is superclass of all java classes. Since it is available for every java class hence Garbage Collector can call finalize method on any java object Now, the finalize method which is present in Object class, has empty implementation, in our class clean-up activities are there, then we have to override this method to define our own clean-up activities.
        There is no guarantee about the time when finalize is called. It may be called any time after the object is not being referred anywhere (cab be garbage collected).
        JVM does not ignore all exceptions while executing finalize method, but it ignores only Unchecked exceptions. If the corresponding catch block is there then JVM won’t ignore and corresponding catch block will be executed.
        System.gc() is just a request to JVM to execute the Garbage Collector. It’s up-to JVM to call Garbage Collector or not.Usually JVM calls Garbage Collector when there is not enough space available in the Heap area or when the memory is low.