Class.forName attempts to load the class whose name is given in its only argument, and returns the Class instance representing that class. In the event that the Class could not be found, resolved, verified, or loaded, Class.forName throws one of several different Exceptions, all of which are listed in the javadoc page for java.lang.Class
How many ways of creating objects in Java?
1) Using clone() method
2) Using Class.forName() and newInstance()
3)Using new operator
Difference between new and Class.forName().newInstance()?
When you use "new" you have to know the name of the class at compile time. You can use newInstance() to instantiate a class that you don't determine until runtime, thus making your code more dynamic.
How can we copy data from one array into another array in single line?
arrayCopy() method of System class copy data from one array into another. The arrayCopy method requires five arguments:
public static void arraycopy(Object source, int srcIndex,Object dest, int destIndex, int length)
How to convert ArrayList as Synchronized?
Java ArrayList is NOT synchronized. To get synchronized list from ArrayList use
static void synchronizedList(List list) method of Collections class.
Ex:
ArrayList arrayList = new ArrayList();
List list = Collections.synchronizedList(arrayList);
java.lang.ClassNotFoundException vs java.lang.NoClassDefFoundError
JAVA throws for fatal issues Error and recoverable issues as Exception. While it comes to class loading exception/error, it is really a myth for developer, who are novice to classloader and its behaviour.
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. This is final method and get called by JVM..
- The loadClass method in class ClassLoader.
Ex:
public class ClassNotFoundExceptionTest {
public static void main(String[] args) {
Class.forName(""); // empty space also throws ClassNotFoundException
ClassLoader.getSystemClassLoader().loadClass("NonExistingClass$subclass"); // non-existing class in classpath
}}
public class ClassNotFoundExceptionTest {
public static void main(String[] args) {
Class.forName(""); // empty space also throws ClassNotFoundException
ClassLoader.getSystemClassLoader().loadClass("NonExistingClass$subclass"); // non-existing class in classpath
}}
NoClassDefFoundError
This error occurs whenever JVM could not able to progress application due to non-existing class reference found in object creation.
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.
Create following classes in a same folder and compile both java files.
MainClass.java
public class MainClass {
public static void main(String[] args) {
Object a= new NonExistingClass();
}
}
NonExistingClass.java
public class NonExistingClass {}