Java67 - Java Program Example Tutorial Blog

Java67 - Java Program Example Tutorial Blog


NoClassDefFoundError vs ClassNotFoundExcepiton in Java

Posted: 10 Dec 2012 05:32 AM PST

Both NoClassDefFoundError and ClassNotFoundException are dreaded Error which comes when JVM or ClassLoader not able to locate class during class loading process. Since different ClassLoader loads classes from different location, sometime this issue may caused because of incorrect CLASSPATH as well i.e. some JAR files from lib is mission or from old version. Though look quite similar there is subtle difference between NoClassDefFoundError and ClassNotFoundException, NoClassDefFoundError indicates that class was present during time of compilation but not available when you run Java program, some time error on static initializer block can also result in NoClassDefFoundError. On the other hand ClassNotFoundException is nothing to do with compile time, ClassNotFoundException comes when you try to load a class in runtime using Reflection, e.g. loading SQL drivers and corresponding Class loader is not able to find this class. Apart from this fundamental difference between NoClassDefFoundError and ClassNoFoundException in Java, let's see some more points for better understanding.
Read more »

Main method Interview Questions in Java - FAQ

Posted: 10 Dec 2012 04:00 AM PST

main method in Java is starting point of any standalone core Java application. JVM starts executing Java program from main method and the thread which executes main is called main thread in Java. Main method is also an important topics in Java interviews for 2 to 3 years experienced developer. In this Java article we will couple of questions related to main method in Java. Apart from Why main is static in Java, I see following questions keep coming related to main method :

Frequently asked question on main method in Java with answers
Can we overload main method in Java ? Which main method JVM will call ?
Can we override main method in Java ?
Can we make main final in Java?
Can we make main synchronized in Java ?
How to call a non static method from main in Java ?

Can we overload main in Java ?
Yes you can overload main method in Java, nothing wrong with this but Java will only call your specific main method, i.e. main method with following signature :
public static void main(String[] args) or  public static void main(String args...) which is main method as variable argument method and only supported post Java 5 world.

Can we override main in Java ?
No you can not overrdie main method in Java, Why? because main is static method and in Java static method is bonded during compile time and you can not override static method in Java. If you decalre method with same name and signature its called method hiding.

Can we make main final in Java?
Ofcourse you can make main method final in Java. JVM has no issue with that. Unlike any final method you can not override main in Java.

Can we make main synchronized in Java?
Yes, main can be synchronized in Java,  synchronized modifier is allowed in main signature and you can make your main method synchronized in Java.

How to call a non static method from main in Java ?
This question applies not only to main but all static methods in Java. Since non static methods can not be called from static context directly, you need to first create an Object as local variable and then you can call non static method using that object, as shown in following example :

import java.util.Date;


/**
 * Java program to show how to call non static method from static method in Java
 *
 * @author http://java67.blogspot.com
 */

public class StaticTest {

    public static void main(String args[]) {
     
        // calling non static method from main in Java
        //printCurrentTime(); //compile time error - can not call non static method from main
     
        StaticTest test = new StaticTest();
        test.printCurrentTime();
     
    }
 
 
    public void printCurrentTime(){
        System.out.println(new Date());
    }
}

Output:
Tue Nov 06 19:07:54 IST 2012

So these were some of the frequently asked questions about main method in Java. This are not only help to answer interview question but also to build your concept on main method in Java.


Other Java Interview questions you may like


Post a Comment