What is the super class for Exception and Error ?


What is Class.forName() ?

The java.lang.Class.forName(String className) method returns the Class object associated with the class or interface with the given string name. Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.

If name denotes a primitive type or void, an attempt will be made to locate a user-defined class in the unnamed package whose name is name. Therefore, this method cannot be used to obtain any of the Class objects representing primitive types or void.

If name denotes an array class, the component type of the array class is loaded but not initialized.

Can interface be final ?

Interfaces are 100% abstract and the only way to create an instance of an interface is to instantiate a class that implements it. Allowing interfaces to be final is completely pointless.

Trying to declare an interface as final in Java results in a compilation error. This is a language design decision – Java interfaces are meant to be extendable.

What is the difference between exception and error ?

An Exception indicates conditions that a reasonable application might want to catch.

An Error indicates serious problems that a reasonable application should not try to catch.

Error and Exception both extend Throwable, but mostly Error is thrown by JVM in a scenario which is fatal and there is no way for the application program to recover from that error. For example OutOfMemoryError.

Though even application can raise an Error but it is just not a good a practice, instead applications should use checked exceptions for recoverable conditions and runtime exceptions for programming errors.

In general error is which nobody can control or guess when it would occur. For example OutOfMemoryError which nobody can guess and can handle it. Exception can be guessed and can be handled in a program. For example if your code is looking for a file which is not available then IOException is thrown. Such instances programmer can guess and can handle it.

What is default value of a local variables ?

The local variables are not initialized to any default value. We should not use local variables without initialization.

What is local class in Java ?

Local classes are defined in a block, which is a group of zero or more statements between balanced braces. We typically find local classes defined in the body of a method. We can define a local class inside any block. For example, we can define a local class in a method body, a for loop, or an if clause.

A local class has access to the members of its enclosing class. In addition, a local class has access to local variables. However, a local class can only access local variables that are declared final. When a local class accesses a local variable or parameter of the enclosing block, it captures that variable or parameter. Starting from Java SE 8, a local class can access local variables and parameters of the enclosing block that are final.

Local classes are similar to inner classes because they cannot define or declare any static members.

Local classes are non-static because they have access to instance members of the enclosing block.

We are supposed not to declare static initializers or member interfaces in a local class.

A local class can have static members provided that they are constant variables.

For example,

package com.roytuts.localclass;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LocalClassExample {

    static String regularExpression = "^([a-zA-Z0-9])+([a-zA-Z0-9\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$";

    public static void validateEmail(String email1, String email2) {

        class EmailAddress {

            Pattern pattern;
            Matcher matcher;

            EmailAddress(String email) {
                pattern = Pattern.compile(regularExpression);
                matcher = pattern.matcher(email);

            public boolean validate() {
                return matcher.matches();


        EmailAddress emailAddress1 = new EmailAddress(email1);
        EmailAddress emailAddress2 = new EmailAddress(email2);

        if (emailAddress1.validate())
            System.out.println(email1 + " is valid.");
            System.out.println(email1 + " is invalid.");

        if (emailAddress2.validate())
            System.out.println(email2 + " is valid.");
            System.out.println(email2 + " is invalid.");


    public static void main(String[] args) {
        validateEmail("soumitra", "soumitrajuster@email.com");



soumitra is invalid.
soumitrajuster@email.com is valid.

Can we initialise uninitialized final variable ?

Yes. We can initialise blank final variable only through constructor. The condition here is the final variable should be non-static.

For example,

package roytuts.com.staticimport;

public class StaticImport {

    private final String str;

    public StaticImport(final String str) {
        this.str = str;

    public static void main(String[] args) {
        StaticImport staticImport = new StaticImport("Hello");




Can we declare abstract method as final ?

No. We cannot declare abstract method as final. We have to provide implementation for abstract method in subclass.

Can we have finally block without catch block ?


For example,

try {
    // code
} finally {
    // cleanup

What is pass by value and pass by reference ?

Pass by value

Passing a copy of the value, not the original reference.

Pass by reference

Passing the address of the object, so that you can access the original object.

Can we declare main method as private ?

You can declare main method as private but when you try to run the class then it will throw an exception in Eclipse similar to below:

Error: Main method not found in class <some class name with full package>, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

What is the difference between preemptive scheduling and time slicing ?

Preemptive scheduling

The highest priority task executes until it enters the waiting or dead states or a higher priority task comes into an existence.

Time slicing scheduling

A task executes for a predefined slice of time and then re-enters the pool of ready tasks.

Can non-static member classes (Local classes) have static members ?

Non-static member classes cannot have static members. Because an object from non-static member class or local class has to be created in the context of the instance of an enclosing class. A local class can have static members provided that they are constant variables.