资源预览内容
第1页 / 共574页
第2页 / 共574页
第3页 / 共574页
第4页 / 共574页
第5页 / 共574页
第6页 / 共574页
第7页 / 共574页
第8页 / 共574页
第9页 / 共574页
第10页 / 共574页
亲,该文档总共574页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
1 Learn Java/J2EE core concepts and key areas With Java/J2EE Job Interview CompanionByK.Arulkumaranimport java.io.File;import java.net.URL;Q 05: Explain Java class loaders? If you have a class in a package, what do you need to do to run it? Explain dynamicclass loading? LF A 05: Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class thatis already running in the JVM. So, how is the very first class loaded? The very first class is especially loaded withthe help of static main( ) method declared in your class. All the subsequently loaded classes are loaded by theclasses, which are already loaded and running. A class loader creates a namespace. All JVMs include at least oneclass loader that is embedded within the JVM called the primordial (or bootstrap) class loader. Now lets look atnon-primordial class loaders. The JVM has hooks in it to allow user defined class loaders to be used in place ofprimordial class loader. Let us look at the class loaders created by the JVM.CLASS LOADER reloadable? ExplanationBootstrap(primordial)No Loads JDK internal classes, java.* packages. (as defined in the sun.boot.class.pathsystem property, typically loads rt.jar and i18n.jar)Extensions No Loads jar files from JDK extensions directory (as defined in the java.ext.dirs systemproperty usually lib/ext directory of the JRE)System No Loads classes from system classpath (as defined by the java.class.path property, whichis set by the CLASSPATH environment variable or classpath or cp command lineoptions)Bootstrap(primordial)(rt.jar, i18.jar)Extensions(lib/ext)System(-classpath)Sibling1classloaderSibling2classloaderJVM class loadersClasses loaded by Bootstrap class loader have no visibility into classesloaded by its descendants (ie Extensions and Systems class loaders).The classes loaded by system class loader have visibility into classes loadedby its parents (ie Extensions and Bootstrap class loaders).If there were any sibling class loaders they cannot see classes loaded byeach other. They can only see the classes loaded by their parent classloader. For example Sibling1 class loader cannot see classes loaded bySibling2 class loaderBoth Sibling1 and Sibling2 class loaders have visibilty into classes loadedby their parent class loaders (eg: System, Extensions, and Bootstrap)Class loaders are hierarchical and use a delegation model when loading a class. Class loaders request theirparent to load the class first before attempting to load it themselves. When a class loader loads a class, the childclass loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loadedJava - Fundamentals 16by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not trueas explained in the above diagram.Q. What do you need to do to run a class with a main() method in a package?Example: Say, you have a class named “Pet” in a project folder “c:myProject” and package namedcom.xyz.client, will you be able to compile and run it as it is?package com.xyz.client;public class Pet public static void main(String args) System.out.println(“I am found in the classpath“);To run c:myProject java com.xyz.client.PetThe answer is no and you will get the following exception: “Exception in thread “main“ java.lang.-NoClassDefFoundError: com/xyz/client/Pet”. You need to set the classpath. How can you do that? One of thefollowing ways:1. Set the operating system CLASSPATH environment variable to have the project folder “c:myProject”. Shownin the above diagram as the System classpath class loader2. Set the operating system CLASSPATH environment variable to have a jar file “c:/myProject/client.jar”, whichhas the Pet.class file in it. Shown in the above diagram as the System classpath class loader.3. Run it with cp or classpath option as shown below:c:java cp c:/myProject com.xyz.client.PetORc:java -classpath c:/myProject/client.jar com.xyz.client.PetImportant: Two objects loaded by different class loaders are never equal even if they carry the same values, which mean aclass is uniquely identified in the context of the associated class loader. This applies to singletons too, where each classloader will have its own singleton. Refer Q51 in Java section for singleton design patternQ. Explain static vs. dynamic class loading?Static class loading Dynamic class loadingClasses are statically loaded with Javas“new” operator.class MyClass public static void main(String args) Car c = new Car();Dynamic loading is a technique for programmatically invoking the functions of aclass loader at run time. Let us look at how to load classes dynamically.Class.forName (String className); /static method which returns a ClassThe above static method returns the class object associated with the classname. The string className can be supplied dynamically at run time. Unlike thestatic loading, the dynamic loading will decide
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号