Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
This page was exported from TechnicalStack [ http://technicalstack.com ] Export date: Wed May 10 16:29:23 2017 / +0000 GMT Class Loader Class Loader : Type of Class Loaders Every Class Loader Subsystem contains the 3 Class Loaders 1. Bootstrap Class Loader/Primordial class Loader 2. Extension Class Loader 3. Application Class Loader/System Class Loader Bootstrap Class Loader/Primordial class Loader This class is responsible for loading Core Java API Classes i.e The classes present in rt.jar Location:Jdkjre7librt.jar This location is called Bootstrap Class Path i.e Bootstrap Class Loader is responsible to load classes from Bootstrap Class Path. Bootstrap Class Loader is present in JVM by Default and it is implemented in native language C and C++ Extension Class Loader: It is the child of Bootstrap Class loader and is responsible to load Class from Location:JDKjre7libext This Class Loader is implemented in Java and the corresponding .class File Name is sun.misc.Launcher$ExtClassLoader.class It also loads various security extension functions. Application Class Loader/System Class Loader It is the Child of Extension Class Loader and responsible to load Classes from Application Classpath,It internally uses Environmental Variable Class Path.It is implemented in Java and the corresponding .class file is at sun.misc.Launcher$ApplicationClassLoader.class Example : class Test { public static void main (String args[])throws Exception{ System.out.println(String.class.classLoader()); System.out.println(Student.class.classLoader()); System.out.println(Test.class.classLoader()); } } Assume that Student.class in present in both extension class loader and application class loader and Test.class is present in only in Application class path. For String Class : From Bootstrap Class path by Bootstrap Class Loader output is null(as Bootstrap Class Loader is not implemented in Java and it is not Java Object) For Student Class : From Extension class path by Extension Class Loader Output is sun.misc.Launcher$ExtClassLoader@1234 For Test Class : From Application Class Path by Application Class Loader Output is sun.misc.Launcher$ AppClassLoader@4567 Note: Bootstrap Class Loader is not Java Object and Hence we are getting null as our first output. Extension Class Loader and Application Class Loader are Java Object and hence we are getting proper output. ClassName@HexaDecimal String of HashCode Class Loader Subsystem will give Highest Priority to Bootstrap Class loader and then Extension Class Loader followed by Application Class Loader. How class loader Works ? 1. Class Loader follows delegation Herecary Principle 2. Whenever JVM comes accross a particular class it will first check weather the corresponding class is already loaded or not. 3. If it is already loaded in Method area then JVM will use that loaded class. 4. If it is not already Loaded then JVM will request Class Loader Subsystem to load that particular class,then Class Loader Subsystem handovers the request 5. to Application Class Loader. 6. Application Class Loader delegate the request to Extenstion Class Loader and Extension Class loader inturn delegate to Bootstrap Class Loader. 7. Bootstrap Class Loader searched in Bootstrap class path (jdk/jre/lib) If the specified class is available then it will load it.Otherwise Bootstrap class Loader delegate it to Extenstion Class Loader. 8. Extension Class Loader searched in the Extensino Class path(jdk/jre/lib/ext).If the specified class is available then it will load it,Otherwise it will delegate the request to Application Class Loader. 9. Application Class Loader searches in Application class path.If the specified class path is already available it will load otherwise it will give Runtime Exception : ClassNotFoundException. What is the need of Custom Class Loader ? Sometimes we may not satisfy with default class loaders and with default class Loader Mechanism, Example : Default class loader will load the ,class file only once even though we are using multiple times that class in our program. After Loading .class file if it is modified Outside the clas the Default Class loader won't load updated class version file on the Fly,because .class file is already available in the method area. We can resolve this problem by defining our own customized class loader. For example whenever we are using a class,first my class loader will check weather updated version is available or not.If it is available then load the updated version otherwise use the existing .class file so that always the updated version is available to our program. The Main Advantage of Customized class loader is that we can customize class loading mechanism based on our requirement. Purpose of java.lang.Classloader Class : We can use ClassLoader Class to create our own Custom ClassLoaders. In Java every Custom ClassLoader should extend the ClassLoader class directly or indirectly. So the ClassLoader Class is the Base Class for all custom ClassLoader Code: public Clas CustomizedClasLoader extends ClassLoader{ public Class loadClass(String className) throws ClassNotFoundException { //Custom logic // check for updated clas version if availabe load the updated .class file and return the updated Class object //else return class object of already loaded Class } } Important : Where to use Customize class loader ? While develoving our own Application Server and WebServers What is the purpose of ClassLoder class ? We can use ClassLoader Class to create our own Custom ClassLoaders. To improve performance while loading class we can go for CustomizedClasLoader. Post date: 2016-06-07 19:31:20 Post date GMT: 2016-06-07 19:31:20 Post modified date: 2016-07-28 11:06:39 Post modified date GMT: 2016-07-28 11:06:39 Powered by [ Universal Post Manager ] plugin. MS Word saving format developed by gVectors Team www.gVectors.com