Sunday, April 26, 2015

Class Loaders In Java


Java class loaders are used to load classes at runtime. ClassLoader in Java works on three principle: Delegation, Visibility and Uniqueness. 

Delegation principle forward request of class loading to parent class loader and only loads the class, if parent is not able to find or load class.

Visibility principle
allows child class loader to see all the classes loaded by parent ClassLoader, but parent class loader can not see classes loaded by child.

Uniqueness principle
allows to load a class exactly once, which is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by parent.

If you have Correct understanding of class loader it will help you to resolve issues like NoClassDefFoundError in Java and java.lang.ClassNotFoundException.

ClassLoader is responsible for loading class files from file system, network or any other source.
There are three default class loader used in Java, Bootstrap , Extension and System/Application class loader.

Every class loader has a predefined location, from where they loads class files.

Bootstrap ClassLoader is responsible for loading standard JDK class files from rt.jar and it is parent of all class loaders in Java. Bootstrap class loader don't have any parents

Extension ClassLoader delegates class loading request to its parent, Bootstrap and if unsuccessful, loads class form jre/lib/ext directory or any other directory pointed by java.ext.dirs system property. Extension ClassLoader in JVM is implemented by  sun.misc.Launcher$ExtClassLoader.

Third default class loader used by JVM to load Java classes is called System or Application class loader and it is responsible for loading application specific classes from CLASSPATH environment.




If you want to load a class Hello.java first the Application ClassLoader will route up to Extension ClassLoader and then the Extension class loader will route up to Bootstrap class loader. the Bootstrap class Loader will look into rt.jar. if found Instance of the class is created else  it will come to extension class loader which will look into jre/lib/ext. If found Instance of the class is created else it will come to System class loader which will look into corresponding classpath . If found Instance of the class is created.


Read More 

Java 8 Features
How to Reset Arraylist In Java
How HashMap Work in Java
Why wait (), notify () and notifyAll () must be called from synchronized block or method in Java
XPath to locate Information in XML
Internals of Garbage Collector
Reference Type in Java
Different Ways to Create ObjectClass Loaders in Java
Producer Consumer Problem
Why String is Final in Java
Singleton Class using Enum
JSON tutorial
Exceptional Handling in Java 

Tuesday, April 7, 2015

Different ways to create a object in Java

 Different ways to create an object in Java


1) Using New Keyword. This is the most common way to create an object in java. Almost 99% of objects are created in this way. It is the most basic way to create an object.

public class CreateObjects {
public static void main(String[] args) {
Create obj = new Create();
}
}
class Create{
String name;
}
2) using Class.forName().
 Class.forName() gives you the class object, which is useful for reflection. The methods that this object has are defined by Java, not by the programmer writing the class. They are the same for every class. Calling newInstance() on that gives you an instance of that class. If we know the name of the class & if it has a public default constructor we can create an object in this way.

MyClass object = (MyClass ) Class.forName("com.abc.MyClass ").newInstance();

3. using clone() .
The clone() can be used to create a copy of an existing object.

MyClass objmyclass= new MyClass ();
MyClass object = objmyclass.clone();

4)  Deserialization
Using object deserialization .Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream ois = new ObjectInputStream(anInputStream );
MyClass object = (MyClass) ois.readObject(); 
5) Factory Method
    By Using Factory Method:
    ClassName ObgRef=ClassName.FactoryMethod();
    Example:    RunTime rt=Runtime.getRunTime();//Static Factory Method.

      
6)  JDK8 should have lambdas (essentially concise anonymous inner classes), which are implicitly converted to objects.
 
      

Read More 

Java 8 Features
How to Reset Arraylist In Java
How HashMap Work in Java
Why wait (), notify () and notifyAll () must be called from synchronized block or method in Java
XPath to locate Information in XML
Internals of Garbage Collector
Reference Type in Java
Different Ways to Create ObjectClass Loaders in Java
Producer Consumer Problem
Why String is Final in Java
Singleton Class using Enum
JSON tutorial
Exceptional Handling in Java