JAVA类的加载(2) ——按需加载(延迟加载)
1、例1:
1 /* 2 按需加载:当你不去实例化Cat时,Cat相关类都不会被加载,即按需加载(需要时加载) 3 1、先加载父类 4 2、初始化类 5 3、类只加载一次(暂且这么认为)——缓存 6 7 加载方法:1、隐式加载 2、显式加载 8 */ 9 public class Example { 10 public static void main(String[] args) throws ClassNotFoundException { 11 System.out.println("\n准备实例化Cat"); 12 Cat cat = new Cat(); //隐式加载,这里先加载父类 13 System.out.println("Cat实例化完成"); 14 15 System.out.println("\n准备加载MyTest类"); 16 System.out.println("第一次调用Class.forname()"); 17 Class.forName("MyTest"); //显式加载类,用Class.forName("com.taobao.ju.MyTest"); 18 System.out.println("第二次调用Class.forname()"); //缓存机制:一旦一个类被载入JVM中,同一个类就不会再被载入了。 19 Class.forName("MyTest"); 20 21 System.out.println("\n准备加载MyTest2类"); //Class.forName()加载类同时 强制初始化类(初始化静态部分) 22 Class.forName("MyTest2"); 23 24 System.out.println("\n准备加载MyTest3类"); 25 ClassLoader c1 = ClassLoader.getSystemClassLoader(); 26 c1.loadClass("MyTest3"); //ClassLoader.loadClass() 该语句仅是加载MyTest3类,并不执行该类的初始化 27 System.out.println("第二次调用ClassLoader.loadClass()"); 28 c1.loadClass("MyTest3"); 29 30 System.out.println("\n程序结束\n"); 31 } 32 } 33 34 class Animal { 35 static { 36 System.out.println("I am an animal"); 37 } 38 } 39 40 class Cat extends Animal { 41 static { 42 System.out.println("I am a cat"); 43 } 44 } 45 46 class MyTest { 47 48 } 49 50 class MyTest2 { 51 static { 52 System.out.println("MyTest2的静态部分初始化"); 53 } 54 } 55 56 class MyTest3 { 57 static { 58 System.out.println("MyTest3的静态初始化"); 59 } 60 }
执行时在eclipse里面加上参数-verbose,能够显示详细的加载信息
结果如下:开始会先加载rt.jar,程序结束后加载java.lang.Shutdown等
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [Opened C:\jdk1.6.0\jre\lib\rt.jar] 2 [Loaded java.lang.Object from C:\jdk1.6.0\jre\lib\rt.jar] 3 [Loaded java.io.Serializable from C:\jdk1.6.0\jre\lib\rt.jar] 4 [Loaded java.lang.Comparable from C:\jdk1.6.0\jre\lib\rt.jar] 5 [Loaded java.lang.CharSequence from C:\jdk1.6.0\jre\lib\rt.jar] 6 [Loaded java.lang.String from C:\jdk1.6.0\jre\lib\rt.jar] 7 [Loaded java.lang.reflect.GenericDeclaration from C:\jdk1.6.0\jre\lib\rt.jar] 8 [Loaded java.lang.reflect.Type from C:\jdk1.6.0\jre\lib\rt.jar] 9 [Loaded java.lang.reflect.AnnotatedElement from C:\jdk1.6.0\jre\lib\rt.jar] 10 [Loaded java.lang.Class from C:\jdk1.6.0\jre\lib\rt.jar] 11 [Loaded java.lang.Cloneable from C:\jdk1.6.0\jre\lib\rt.jar] 12 [Loaded java.lang.ClassLoader from C:\jdk1.6.0\jre\lib\rt.jar] 13 [Loaded java.lang.System from C:\jdk1.6.0\jre\lib\rt.jar] 14 [Loaded java.lang.Throwable from C:\jdk1.6.0\jre\lib\rt.jar] 15 [Loaded java.lang.Error from C:\jdk1.6.0\jre\lib\rt.jar] 16 [Loaded java.lang.ThreadDeath from C:\jdk1.6.0\jre\lib\rt.jar] 17 [Loaded java.lang.Exception from C:\jdk1.6.0\jre\lib\rt.jar] 18 [Loaded java.lang.RuntimeException from C:\jdk1.6.0\jre\lib\rt.jar] 19 [Loaded java.security.ProtectionDomain from C:\jdk1.6.0\jre\lib\rt.jar] 20 [Loaded java.security.AccessControlContext from C:\jdk1.6.0\jre\lib\rt.jar] 21 [Loaded java.lang.ClassNotFoundException from C:\jdk1.6.0\jre\lib\rt.jar] 22 [Loaded java.lang.LinkageError from C:\jdk1.6.0\jre\lib\rt.jar] 23 [Loaded java.lang.NoClassDefFoundError from C:\jdk1.6.0\jre\lib\rt.jar] 24 [Loaded java.lang.ClassCastException from C:\jdk1.6.0\jre\lib\rt.jar] 25 [Loaded java.lang.ArrayStoreException from C:\jdk1.6.0\jre\lib\rt.jar] 26 [Loaded java.lang.VirtualMachineError from C:\jdk1.6.0\jre\lib\rt.jar] 27 [Loaded java.lang.OutOfMemoryError from C:\jdk1.6.0\jre\lib\rt.jar] 28 [Loaded java.lang.StackOverflowError from C:\jdk1.6.0\jre\lib\rt.jar] 29 [Loaded java.lang.IllegalMonitorStateException from C:\jdk1.6.0\jre\lib\rt.jar] 30 [Loaded java.lang.ref.Reference from C:\jdk1.6.0\jre\lib\rt.jar] 31 [Loaded java.lang.ref.SoftReference from C:\jdk1.6.0\jre\lib\rt.jar] 32 [Loaded java.lang.ref.WeakReference from C:\jdk1.6.0\jre\lib\rt.jar] 33 [Loaded java.lang.ref.FinalReference from C:\jdk1.6.0\jre\lib\rt.jar] 34 [Loaded java.lang.ref.PhantomReference from C:\jdk1.6.0\jre\lib\rt.jar] 35 [Loaded java.lang.ref.Finalizer from C:\jdk1.6.0\jre\lib\rt.jar] 36 [Loaded java.lang.Runnable from C:\jdk1.6.0\jre\lib\rt.jar] 37 [Loaded java.lang.Thread from C:\jdk1.6.0\jre\lib\rt.jar] 38 [Loaded java.lang.Thread$UncaughtExceptionHandler from C:\jdk1.6.0\jre\lib\rt.jar] 39 [Loaded java.lang.ThreadGroup from C:\jdk1.6.0\jre\lib\rt.jar] 40 [Loaded java.util.Map from C:\jdk1.6.0\jre\lib\rt.jar] 41 [Loaded java.util.Dictionary from C:\jdk1.6.0\jre\lib\rt.jar] 42 [Loaded java.util.Hashtable from C:\jdk1.6.0\jre\lib\rt.jar] 43 [Loaded java.util.Properties from C:\jdk1.6.0\jre\lib\rt.jar] 44 [Loaded java.lang.reflect.AccessibleObject from C:\jdk1.6.0\jre\lib\rt.jar] 45 [Loaded java.lang.reflect.Member from C:\jdk1.6.0\jre\lib\rt.jar] 46 [Loaded java.lang.reflect.Field from C:\jdk1.6.0\jre\lib\rt.jar] 47 [Loaded java.lang.reflect.Method from C:\jdk1.6.0\jre\lib\rt.jar] 48 [Loaded java.lang.reflect.Constructor from C:\jdk1.6.0\jre\lib\rt.jar] 49 [Loaded sun.reflect.MagicAccessorImpl from C:\jdk1.6.0\jre\lib\rt.jar] 50 [Loaded sun.reflect.MethodAccessor from C:\jdk1.6.0\jre\lib\rt.jar] 51 [Loaded sun.reflect.MethodAccessorImpl from C:\jdk1.6.0\jre\lib\rt.jar] 52 [Loaded sun.reflect.ConstructorAccessor from C:\jdk1.6.0\jre\lib\rt.jar] 53 [Loaded sun.reflect.ConstructorAccessorImpl from C:\jdk1.6.0\jre\lib\rt.jar] 54 [Loaded sun.reflect.DelegatingClassLoader from C:\jdk1.6.0\jre\lib\rt.jar] 55 [Loaded sun.reflect.ConstantPool from C:\jdk1.6.0\jre\lib\rt.jar] 56 [Loaded sun.reflect.FieldAccessor from C:\jdk1.6.0\jre\lib\rt.jar] 57 [Loaded sun.reflect.FieldAccessorImpl from C:\jdk1.6.0\jre\lib\rt.jar] 58 [Loaded sun.reflect.UnsafeFieldAccessorImpl from C:\jdk1.6.0\jre\lib\rt.jar] 59 [Loaded sun.reflect.UnsafeStaticFieldAccessorImpl from C:\jdk1.6.0\jre\lib\rt.jar] 60 [Loaded java.lang.Appendable from C:\jdk1.6.0\jre\lib\rt.jar] 61 [Loaded java.lang.AbstractStringBuilder from C:\jdk1.6.0\jre\lib\rt.jar] 62 [Loaded java.lang.StringBuffer from C:\jdk1.6.0\jre\lib\rt.jar] 63 [Loaded java.lang.StringBuilder from C:\jdk1.6.0\jre\lib\rt.jar] 64 [Loaded java.lang.StackTraceElement from C:\jdk1.6.0\jre\lib\rt.jar] 65 [Loaded java.nio.Buffer from C:\jdk1.6.0\jre\lib\rt.jar] 66 [Loaded sun.misc.AtomicLong from C:\jdk1.6.0\jre\lib\rt.jar] 67 [Loaded sun.misc.AtomicLongCSImpl from C:\jdk1.6.0\jre\lib\rt.jar] 68 [Loaded sun.misc.PostVMInitHook from C:\jdk1.6.0\jre\lib\rt.jar] 69 [Loaded java.lang.Boolean from C:\jdk1.6.0\jre\lib\rt.jar] 70 [Loaded java.lang.Character from C:\jdk1.6.0\jre\lib\rt.jar] 71 [Loaded java.lang.Number from C:\jdk1.6.0\jre\lib\rt.jar] 72 [Loaded java.lang.Float from C:\jdk1.6.0\jre\lib\rt.jar] 73 [Loaded java.lang.Double from C:\jdk1.6.0\jre\lib\rt.jar] 74 [Loaded java.lang.Byte from C:\jdk1.6.0\jre\lib\rt.jar] 75 [Loaded java.lang.Short from C:\jdk1.6.0\jre\lib\rt.jar] 76 [Loaded java.lang.Integer from C:\jdk1.6.0\jre\lib\rt.jar] 77 [Loaded java.lang.Long from C:\jdk1.6.0\jre\lib\rt.jar] 78 [Loaded java.lang.NullPointerException from C:\jdk1.6.0\jre\lib\rt.jar] 79 [Loaded java.lang.ArithmeticException from C:\jdk1.6.0\jre\lib\rt.jar] 80 [Loaded java.io.ObjectStreamField from C:\jdk1.6.0\jre\lib\rt.jar] 81 [Loaded java.util.Comparator from C:\jdk1.6.0\jre\lib\rt.jar] 82 [Loaded java.lang.String$CaseInsensitiveComparator from C:\jdk1.6.0\jre\lib\rt.jar] 83 [Loaded java.security.Guard from C:\jdk1.6.0\jre\lib\rt.jar] 84 [Loaded java.security.Permission from C:\jdk1.6.0\jre\lib\rt.jar] 85 [Loaded java.security.BasicPermission from C:\jdk1.6.0\jre\lib\rt.jar] 86 [Loaded java.lang.RuntimePermission from C:\jdk1.6.0\jre\lib\rt.jar] 87 [Loaded java.util.AbstractMap from C:\jdk1.6.0\jre\lib\rt.jar] 88 [Loaded sun.misc.SoftCache from C:\jdk1.6.0\jre\lib\rt.jar] 89 [Loaded java.lang.ref.ReferenceQueue from C:\jdk1.6.0\jre\lib\rt.jar] 90 [Loaded java.lang.ref.ReferenceQueue$Null from C:\jdk1.6.0\jre\lib\rt.jar] 91 [Loaded java.lang.ref.ReferenceQueue$Lock from C:\jdk1.6.0\jre\lib\rt.jar] 92 [Loaded java.util.HashMap from C:\jdk1.6.0\jre\lib\rt.jar] 93 [Loaded java.util.Map$Entry from C:\jdk1.6.0\jre\lib\rt.jar] 94 [Loaded java.util.HashMap$Entry from C:\jdk1.6.0\jre\lib\rt.jar] 95 [Loaded java.security.AccessController from C:\jdk1.6.0\jre\lib\rt.jar] 96 [Loaded sun.misc.JavaSecurityAccess from C:\jdk1.6.0\jre\lib\rt.jar] 97 [Loaded java.security.AccessControlContext$1 from C:\jdk1.6.0\jre\lib\rt.jar] 98 [Loaded sun.misc.SharedSecrets from C:\jdk1.6.0\jre\lib\rt.jar] 99 [Loaded sun.misc.Unsafe from C:\jdk1.6.0\jre\lib\rt.jar] 100 [Loaded java.lang.IncompatibleClassChangeError from C:\jdk1.6.0\jre\lib\rt.jar] 101 [Loaded java.lang.NoSuchMethodError from C:\jdk1.6.0\jre\lib\rt.jar] 102 [Loaded sun.reflect.Reflection from C:\jdk1.6.0\jre\lib\rt.jar] 103 [Loaded java.util.Collections from C:\jdk1.6.0\jre\lib\rt.jar] 104 [Loaded java.lang.Iterable from C:\jdk1.6.0\jre\lib\rt.jar] 105 [Loaded java.util.Collection from C:\jdk1.6.0\jre\lib\rt.jar] 106 [Loaded java.util.Set from C:\jdk1.6.0\jre\lib\rt.jar] 107 [Loaded java.util.AbstractCollection from C:\jdk1.6.0\jre\lib\rt.jar] 108 [Loaded java.util.AbstractSet from C:\jdk1.6.0\jre\lib\rt.jar] 109 [Loaded java.util.Collections$EmptySet from C:\jdk1.6.0\jre\lib\rt.jar] 110 [Loaded java.util.RandomAccess from C:\jdk1.6.0\jre\lib\rt.jar] 111 [Loaded java.util.List from C:\jdk1.6.0\jre\lib\rt.jar] 112 [Loaded java.util.AbstractList from C:\jdk1.6.0\jre\lib\rt.jar] 113 [Loaded java.util.Collections$EmptyList from C:\jdk1.6.0\jre\lib\rt.jar] 114 [Loaded java.util.Collections$EmptyMap from C:\jdk1.6.0\jre\lib\rt.jar] 115 [Loaded java.util.Collections$ReverseComparator from C:\jdk1.6.0\jre\lib\rt.jar] 116 [Loaded java.util.Collections$SynchronizedMap from C:\jdk1.6.0\jre\lib\rt.jar] 117 [Loaded java.lang.reflect.ReflectPermission from C:\jdk1.6.0\jre\lib\rt.jar] 118 [Loaded java.security.PrivilegedAction from C:\jdk1.6.0\jre\lib\rt.jar] 119 [Loaded sun.reflect.ReflectionFactory$GetReflectionFactoryAction from C:\jdk1.6.0\jre\lib\rt.jar] 120 [Loaded java.util.Vector from C:\jdk1.6.0\jre\lib\rt.jar] 121 [Loaded java.util.Stack from C:\jdk1.6.0\jre\lib\rt.jar] 122 [Loaded sun.reflect.ReflectionFactory from C:\jdk1.6.0\jre\lib\rt.jar] 123 [Loaded java.lang.ref.Reference$Lock from C:\jdk1.6.0\jre\lib\rt.jar] 124 [Loaded java.lang.ref.Reference$ReferenceHandler from C:\jdk1.6.0\jre\lib\rt.jar] 125 [Loaded java.lang.ref.Finalizer$FinalizerThread from C:\jdk1.6.0\jre\lib\rt.jar] 126 [Loaded java.util.Enumeration from C:\jdk1.6.0\jre\lib\rt.jar] 127 [Loaded java.util.Hashtable$EmptyEnumerator from C:\jdk1.6.0\jre\lib\rt.jar] 128 [Loaded java.util.Iterator from C:\jdk1.6.0\jre\lib\rt.jar] 129 [Loaded java.util.Hashtable$EmptyIterator from C:\jdk1.6.0\jre\lib\rt.jar] 130 [Loaded java.util.Hashtable$Entry from C:\jdk1.6.0\jre\lib\rt.jar] 131 [Loaded java.nio.charset.Charset from C:\jdk1.6.0\jre\lib\rt.jar] 132 [Loaded java.nio.charset.spi.CharsetProvider from C:\jdk1.6.0\jre\lib\rt.jar] 133 [Loaded sun.nio.cs.FastCharsetProvider from C:\jdk1.6.0\jre\lib\rt.jar] 134 [Loaded sun.nio.cs.StandardCharsets from C:\jdk1.6.0\jre\lib\rt.jar] 135 [Loaded sun.util.PreHashedMap from C:\jdk1.6.0\jre\lib\rt.jar] 136 [Loaded sun.nio.cs.StandardCharsets$Aliases from C:\jdk1.6.0\jre\lib\rt.jar] 137 [Loaded sun.nio.cs.StandardCharsets$Classes from C:\jdk1.6.0\jre\lib\rt.jar] 138 [Loaded sun.nio.cs.StandardCharsets$Cache from C:\jdk1.6.0\jre\lib\rt.jar] 139 [Loaded java.lang.ThreadLocal from C:\jdk1.6.0\jre\lib\rt.jar] 140 [Loaded java.util.concurrent.atomic.AtomicInteger from C:\jdk1.6.0\jre\lib\rt.jar] 141 [Loaded java.lang.Class$3 from C:\jdk1.6.0\jre\lib\rt.jar] 142 [Loaded java.lang.reflect.Modifier from C:\jdk1.6.0\jre\lib\rt.jar] 143 [Loaded sun.reflect.LangReflectAccess from C:\jdk1.6.0\jre\lib\rt.jar] 144 [Loaded java.lang.reflect.ReflectAccess from C:\jdk1.6.0\jre\lib\rt.jar] 145 [Loaded java.lang.StringValue from C:\jdk1.6.0\jre\lib\rt.jar] 146 [Loaded java.util.Arrays from C:\jdk1.6.0\jre\lib\rt.jar] 147 [Loaded java.lang.Math from C:\jdk1.6.0\jre\lib\rt.jar] 148 [Loaded java.nio.charset.Charset$3 from C:\jdk1.6.0\jre\lib\rt.jar] 149 [Opened C:\jdk1.6.0\jre\lib\charsets.jar] 150 [Loaded sun.nio.cs.AbstractCharsetProvider from C:\jdk1.6.0\jre\lib\rt.jar] 151 [Loaded sun.nio.cs.ext.ExtendedCharsets from C:\jdk1.6.0\jre\lib\charsets.jar] 152 [Loaded java.lang.Class$1 from C:\jdk1.6.0\jre\lib\rt.jar] 153 [Loaded sun.reflect.ReflectionFactory$1 from C:\jdk1.6.0\jre\lib\rt.jar] 154 [Loaded sun.reflect.NativeConstructorAccessorImpl from C:\jdk1.6.0\jre\lib\rt.jar] 155 [Loaded sun.reflect.DelegatingConstructorAccessorImpl from C:\jdk1.6.0\jre\lib\rt.jar] 156 [Loaded java.util.SortedMap from C:\jdk1.6.0\jre\lib\rt.jar] 157 [Loaded java.util.NavigableMap from C:\jdk1.6.0\jre\lib\rt.jar] 158 [Loaded java.util.TreeMap from C:\jdk1.6.0\jre\lib\rt.jar] 159 [Loaded sun.misc.ASCIICaseInsensitiveComparator from C:\jdk1.6.0\jre\lib\rt.jar] 160 [Loaded java.util.TreeMap$Entry from C:\jdk1.6.0\jre\lib\rt.jar] 161 [Loaded sun.misc.VM from C:\jdk1.6.0\jre\lib\rt.jar] 162 [Loaded sun.nio.cs.ext.GBK from C:\jdk1.6.0\jre\lib\charsets.jar] 163 [Loaded java.lang.StringCoding from C:\jdk1.6.0\jre\lib\rt.jar] 164 [Loaded java.lang.ThreadLocal$ThreadLocalMap from C:\jdk1.6.0\jre\lib\rt.jar] 165 [Loaded java.lang.ThreadLocal$ThreadLocalMap$Entry from C:\jdk1.6.0\jre\lib\rt.jar] 166 [Loaded java.lang.StringCoding$StringDecoder from C:\jdk1.6.0\jre\lib\rt.jar] 167 [Loaded java.nio.charset.CharsetDecoder from C:\jdk1.6.0\jre\lib\rt.jar] 168 [Loaded sun.nio.cs.ext.DoubleByteDecoder from C:\jdk1.6.0\jre\lib\charsets.jar] 169 [Loaded sun.nio.cs.ext.GBK$Decoder from C:\jdk1.6.0\jre\lib\charsets.jar] 170 [Loaded java.nio.charset.CodingErrorAction from C:\jdk1.6.0\jre\lib\rt.jar] 171 [Loaded java.nio.ByteBuffer from C:\jdk1.6.0\jre\lib\rt.jar] 172 [Loaded java.nio.HeapByteBuffer from C:\jdk1.6.0\jre\lib\rt.jar] 173 [Loaded java.nio.Bits from C:\jdk1.6.0\jre\lib\rt.jar] 174 [Loaded sun.misc.JavaNioAccess from C:\jdk1.6.0\jre\lib\rt.jar] 175 [Loaded java.nio.Bits$1 from C:\jdk1.6.0\jre\lib\rt.jar] 176 [Loaded java.nio.ByteOrder from C:\jdk1.6.0\jre\lib\rt.jar] 177 [Loaded java.lang.Readable from C:\jdk1.6.0\jre\lib\rt.jar] 178 [Loaded java.nio.CharBuffer from C:\jdk1.6.0\jre\lib\rt.jar] 179 [Loaded java.nio.HeapCharBuffer from C:\jdk1.6.0\jre\lib\rt.jar] 180 [Loaded java.nio.charset.CoderResult from C:\jdk1.6.0\jre\lib\rt.jar] 181 [Loaded java.nio.charset.CoderResult$Cache from C:\jdk1.6.0\jre\lib\rt.jar] 182 [Loaded java.nio.charset.CoderResult$1 from C:\jdk1.6.0\jre\lib\rt.jar] 183 [Loaded java.nio.charset.CoderResult$2 from C:\jdk1.6.0\jre\lib\rt.jar] 184 [Loaded sun.misc.Version from C:\jdk1.6.0\jre\lib\rt.jar] 185 [Loaded sun.misc.JavaLangAccess from C:\jdk1.6.0\jre\lib\rt.jar] 186 [Loaded java.lang.System$2 from C:\jdk1.6.0\jre\lib\rt.jar] 187 [Loaded java.lang.Runtime from C:\jdk1.6.0\jre\lib\rt.jar] 188 [Loaded java.io.File from C:\jdk1.6.0\jre\lib\rt.jar] 189 [Loaded java.io.FileSystem from C:\jdk1.6.0\jre\lib\rt.jar] 190 [Loaded java.io.Win32FileSystem from C:\jdk1.6.0\jre\lib\rt.jar] 191 [Loaded java.io.WinNTFileSystem from C:\jdk1.6.0\jre\lib\rt.jar] 192 [Loaded java.io.ExpiringCache from C:\jdk1.6.0\jre\lib\rt.jar] 193 [Loaded java.util.LinkedHashMap from C:\jdk1.6.0\jre\lib\rt.jar] 194 [Loaded java.io.ExpiringCache$1 from C:\jdk1.6.0\jre\lib\rt.jar] 195 [Loaded java.util.LinkedHashMap$Entry from C:\jdk1.6.0\jre\lib\rt.jar] 196 [Loaded sun.security.action.GetPropertyAction from C:\jdk1.6.0\jre\lib\rt.jar] 197 [Loaded sun.jkernel.DownloadManager from C:\jdk1.6.0\jre\lib\rt.jar] 198 [Loaded sun.jkernel.DownloadManager$1 from C:\jdk1.6.0\jre\lib\rt.jar] 199 [Loaded sun.jkernel.DownloadManager$2 from C:\jdk1.6.0\jre\lib\rt.jar] 200 [Loaded java.lang.ClassLoader$3 from C:\jdk1.6.0\jre\lib\rt.jar] 201 [Loaded java.io.ExpiringCache$Entry from C:\jdk1.6.0\jre\lib\rt.jar] 202 [Loaded java.lang.ClassLoader$NativeLibrary from C:\jdk1.6.0\jre\lib\rt.jar] 203 [Loaded java.lang.StringCoding$StringEncoder from C:\jdk1.6.0\jre\lib\rt.jar] 204 [Loaded java.nio.charset.CharsetEncoder from C:\jdk1.6.0\jre\lib\rt.jar] 205 [Loaded sun.nio.cs.ext.DoubleByteEncoder from C:\jdk1.6.0\jre\lib\charsets.jar] 206 [Loaded sun.nio.cs.ext.GBK$Encoder from C:\jdk1.6.0\jre\lib\charsets.jar] 207 [Loaded sun.nio.cs.Surrogate$Parser from C:\jdk1.6.0\jre\lib\rt.jar] 208 [Loaded sun.nio.cs.Surrogate from C:\jdk1.6.0\jre\lib\rt.jar] 209 [Loaded java.io.Closeable from C:\jdk1.6.0\jre\lib\rt.jar] 210 [Loaded java.io.InputStream from C:\jdk1.6.0\jre\lib\rt.jar] 211 [Loaded java.io.FileInputStream from C:\jdk1.6.0\jre\lib\rt.jar] 212 [Loaded java.io.FileDescriptor from C:\jdk1.6.0\jre\lib\rt.jar] 213 [Loaded java.io.Flushable from C:\jdk1.6.0\jre\lib\rt.jar] 214 [Loaded java.io.OutputStream from C:\jdk1.6.0\jre\lib\rt.jar] 215 [Loaded java.io.FileOutputStream from C:\jdk1.6.0\jre\lib\rt.jar] 216 [Loaded java.io.FilterInputStream from C:\jdk1.6.0\jre\lib\rt.jar] 217 [Loaded java.io.BufferedInputStream from C:\jdk1.6.0\jre\lib\rt.jar] 218 [Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater from C:\jdk1.6.0\jre\lib\rt.jar] 219 [Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl from C:\jdk1.6.0\jre\lib\rt.jar] 220 [Loaded sun.reflect.misc.ReflectUtil from C:\jdk1.6.0\jre\lib\rt.jar] 221 [Loaded java.io.FilterOutputStream from C:\jdk1.6.0\jre\lib\rt.jar] 222 [Loaded java.io.PrintStream from C:\jdk1.6.0\jre\lib\rt.jar] 223 [Loaded java.io.BufferedOutputStream from C:\jdk1.6.0\jre\lib\rt.jar] 224 [Loaded java.io.Writer from C:\jdk1.6.0\jre\lib\rt.jar] 225 [Loaded java.io.OutputStreamWriter from C:\jdk1.6.0\jre\lib\rt.jar] 226 [Loaded sun.nio.cs.StreamEncoder from C:\jdk1.6.0\jre\lib\rt.jar] 227 [Loaded java.io.BufferedWriter from C:\jdk1.6.0\jre\lib\rt.jar] 228 [Loaded java.lang.Terminator from C:\jdk1.6.0\jre\lib\rt.jar] 229 [Loaded sun.misc.SignalHandler from C:\jdk1.6.0\jre\lib\rt.jar] 230 [Loaded java.lang.Terminator$1 from C:\jdk1.6.0\jre\lib\rt.jar] 231 [Loaded sun.misc.Signal from C:\jdk1.6.0\jre\lib\rt.jar] 232 [Loaded sun.misc.NativeSignalHandler from C:\jdk1.6.0\jre\lib\rt.jar] 233 [Loaded sun.misc.OSEnvironment from C:\jdk1.6.0\jre\lib\rt.jar] 234 [Loaded sun.io.Win32ErrorMode from C:\jdk1.6.0\jre\lib\rt.jar] 235 [Loaded java.lang.Compiler from C:\jdk1.6.0\jre\lib\rt.jar] 236 [Loaded java.lang.Compiler$1 from C:\jdk1.6.0\jre\lib\rt.jar] 237 [Loaded sun.misc.Launcher from C:\jdk1.6.0\jre\lib\rt.jar] 238 [Loaded java.net.URLStreamHandlerFactory from C:\jdk1.6.0\jre\lib\rt.jar] 239 [Loaded sun.misc.Launcher$Factory from C:\jdk1.6.0\jre\lib\rt.jar] 240 [Loaded java.security.SecureClassLoader from C:\jdk1.6.0\jre\lib\rt.jar] 241 [Loaded java.net.URLClassLoader from C:\jdk1.6.0\jre\lib\rt.jar] 242 [Loaded sun.misc.Launcher$ExtClassLoader from C:\jdk1.6.0\jre\lib\rt.jar] 243 [Loaded sun.security.util.Debug from C:\jdk1.6.0\jre\lib\rt.jar] 244 [Loaded java.lang.Package from C:\jdk1.6.0\jre\lib\rt.jar] 245 [Loaded java.net.URL from C:\jdk1.6.0\jre\lib\rt.jar] 246 [Loaded java.security.CodeSource from C:\jdk1.6.0\jre\lib\rt.jar] 247 [Loaded java.lang.Void from C:\jdk1.6.0\jre\lib\rt.jar] 248 [Loaded java.security.cert.Certificate from C:\jdk1.6.0\jre\lib\rt.jar] 249 [Loaded java.lang.ClassFormatError from C:\jdk1.6.0\jre\lib\rt.jar] 250 [Loaded java.lang.IllegalArgumentException from C:\jdk1.6.0\jre\lib\rt.jar] 251 [Loaded java.io.IOException from C:\jdk1.6.0\jre\lib\rt.jar] 252 [Loaded sun.misc.URLClassPath from C:\jdk1.6.0\jre\lib\rt.jar] 253 [Loaded java.lang.AssertionStatusDirectives from C:\jdk1.6.0\jre\lib\rt.jar] 254 [Loaded sun.misc.JavaNetAccess from C:\jdk1.6.0\jre\lib\rt.jar] 255 [Loaded java.net.URLClassLoader$7 from C:\jdk1.6.0\jre\lib\rt.jar] 256 [Loaded java.security.PermissionCollection from C:\jdk1.6.0\jre\lib\rt.jar] 257 [Loaded java.util.StringTokenizer from C:\jdk1.6.0\jre\lib\rt.jar] 258 [Loaded java.security.PrivilegedExceptionAction from C:\jdk1.6.0\jre\lib\rt.jar] 259 [Loaded sun.misc.Launcher$ExtClassLoader$1 from C:\jdk1.6.0\jre\lib\rt.jar] 260 [Loaded sun.misc.MetaIndex from C:\jdk1.6.0\jre\lib\rt.jar] 261 [Loaded java.io.Reader from C:\jdk1.6.0\jre\lib\rt.jar] 262 [Loaded java.io.BufferedReader from C:\jdk1.6.0\jre\lib\rt.jar] 263 [Loaded java.io.InputStreamReader from C:\jdk1.6.0\jre\lib\rt.jar] 264 [Loaded java.io.FileReader from C:\jdk1.6.0\jre\lib\rt.jar] 265 [Loaded sun.nio.cs.StreamDecoder from C:\jdk1.6.0\jre\lib\rt.jar] 266 [Loaded java.util.ArrayList from C:\jdk1.6.0\jre\lib\rt.jar] 267 [Loaded java.lang.reflect.Array from C:\jdk1.6.0\jre\lib\rt.jar] 268 [Loaded java.util.Locale from C:\jdk1.6.0\jre\lib\rt.jar] 269 [Loaded java.util.concurrent.ConcurrentMap from C:\jdk1.6.0\jre\lib\rt.jar] 270 [Loaded java.util.concurrent.ConcurrentHashMap from C:\jdk1.6.0\jre\lib\rt.jar] 271 [Loaded java.util.concurrent.locks.Lock from C:\jdk1.6.0\jre\lib\rt.jar] 272 [Loaded java.util.concurrent.locks.ReentrantLock from C:\jdk1.6.0\jre\lib\rt.jar] 273 [Loaded java.util.concurrent.ConcurrentHashMap$Segment from C:\jdk1.6.0\jre\lib\rt.jar] 274 [Loaded java.util.concurrent.locks.AbstractOwnableSynchronizer from C:\jdk1.6.0\jre\lib\rt.jar] 275 [Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer from C:\jdk1.6.0\jre\lib\rt.jar] 276 [Loaded java.util.concurrent.locks.ReentrantLock$Sync from C:\jdk1.6.0\jre\lib\rt.jar] 277 [Loaded java.util.concurrent.locks.ReentrantLock$NonfairSync from C:\jdk1.6.0\jre\lib\rt.jar] 278 [Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer$Node from C:\jdk1.6.0\jre\lib\rt.jar] 279 [Loaded java.util.concurrent.ConcurrentHashMap$HashEntry from C:\jdk1.6.0\jre\lib\rt.jar] 280 [Loaded java.lang.CharacterDataLatin1 from C:\jdk1.6.0\jre\lib\rt.jar] 281 [Loaded java.io.ObjectStreamClass from C:\jdk1.6.0\jre\lib\rt.jar] 282 [Loaded sun.net.www.ParseUtil from C:\jdk1.6.0\jre\lib\rt.jar] 283 [Loaded java.util.BitSet from C:\jdk1.6.0\jre\lib\rt.jar] 284 [Loaded java.net.Parts from C:\jdk1.6.0\jre\lib\rt.jar] 285 [Loaded java.net.URLStreamHandler from C:\jdk1.6.0\jre\lib\rt.jar] 286 [Loaded sun.net.www.protocol.file.Handler from C:\jdk1.6.0\jre\lib\rt.jar] 287 [Loaded java.util.HashSet from C:\jdk1.6.0\jre\lib\rt.jar] 288 [Loaded sun.net.www.protocol.jar.Handler from C:\jdk1.6.0\jre\lib\rt.jar] 289 [Loaded sun.misc.Launcher$AppClassLoader from C:\jdk1.6.0\jre\lib\rt.jar] 290 [Loaded sun.misc.Launcher$AppClassLoader$1 from C:\jdk1.6.0\jre\lib\rt.jar] 291 [Loaded java.lang.SystemClassLoaderAction from C:\jdk1.6.0\jre\lib\rt.jar] 292 [Loaded java.net.URLClassLoader$1 from C:\jdk1.6.0\jre\lib\rt.jar] 293 [Loaded sun.net.util.URLUtil from C:\jdk1.6.0\jre\lib\rt.jar] 294 [Loaded sun.misc.URLClassPath$3 from C:\jdk1.6.0\jre\lib\rt.jar] 295 [Loaded sun.misc.URLClassPath$Loader from C:\jdk1.6.0\jre\lib\rt.jar] 296 [Loaded sun.misc.URLClassPath$JarLoader from C:\jdk1.6.0\jre\lib\rt.jar] 297 [Loaded sun.misc.URLClassPath$JarLoader$1 from C:\jdk1.6.0\jre\lib\rt.jar] 298 [Loaded sun.misc.FileURLMapper from C:\jdk1.6.0\jre\lib\rt.jar] 299 [Loaded java.util.zip.ZipConstants from C:\jdk1.6.0\jre\lib\rt.jar] 300 [Loaded java.util.zip.ZipFile from C:\jdk1.6.0\jre\lib\rt.jar] 301 [Loaded java.util.jar.JarFile from C:\jdk1.6.0\jre\lib\rt.jar] 302 [Loaded sun.misc.JavaUtilJarAccess from C:\jdk1.6.0\jre\lib\rt.jar] 303 [Loaded java.util.jar.JavaUtilJarAccessImpl from C:\jdk1.6.0\jre\lib\rt.jar] 304 [Loaded sun.misc.JarIndex from C:\jdk1.6.0\jre\lib\rt.jar] 305 [Loaded sun.misc.ExtensionDependency from C:\jdk1.6.0\jre\lib\rt.jar] 306 [Loaded java.util.zip.ZipEntry from C:\jdk1.6.0\jre\lib\rt.jar] 307 [Loaded java.util.jar.JarEntry from C:\jdk1.6.0\jre\lib\rt.jar] 308 [Loaded java.util.jar.JarFile$JarFileEntry from C:\jdk1.6.0\jre\lib\rt.jar] 309 [Loaded java.io.DataInput from C:\jdk1.6.0\jre\lib\rt.jar] 310 [Loaded java.io.DataInputStream from C:\jdk1.6.0\jre\lib\rt.jar] 311 [Loaded java.util.zip.ZipFile$ZipFileInputStream from C:\jdk1.6.0\jre\lib\rt.jar] 312 [Loaded java.util.zip.InflaterInputStream from C:\jdk1.6.0\jre\lib\rt.jar] 313 [Loaded java.util.zip.ZipFile$1 from C:\jdk1.6.0\jre\lib\rt.jar] 314 [Loaded java.util.zip.Inflater from C:\jdk1.6.0\jre\lib\rt.jar] 315 [Loaded java.util.zip.ZStreamRef from C:\jdk1.6.0\jre\lib\rt.jar] 316 [Loaded java.security.PrivilegedActionException from C:\jdk1.6.0\jre\lib\rt.jar] 317 [Loaded sun.misc.URLClassPath$FileLoader from C:\jdk1.6.0\jre\lib\rt.jar] 318 [Loaded sun.misc.Resource from C:\jdk1.6.0\jre\lib\rt.jar] 319 [Loaded sun.misc.URLClassPath$FileLoader$1 from C:\jdk1.6.0\jre\lib\rt.jar] 320 [Loaded sun.nio.ByteBuffered from C:\jdk1.6.0\jre\lib\rt.jar] 321 [Loaded java.security.Permissions from C:\jdk1.6.0\jre\lib\rt.jar] 322 [Loaded java.net.URLConnection from C:\jdk1.6.0\jre\lib\rt.jar] 323 [Loaded sun.net.www.URLConnection from C:\jdk1.6.0\jre\lib\rt.jar] 324 [Loaded sun.net.www.protocol.file.FileURLConnection from C:\jdk1.6.0\jre\lib\rt.jar] 325 [Loaded java.net.ContentHandler from C:\jdk1.6.0\jre\lib\rt.jar] 326 [Loaded java.net.UnknownContentHandler from C:\jdk1.6.0\jre\lib\rt.jar] 327 [Loaded sun.net.www.MessageHeader from C:\jdk1.6.0\jre\lib\rt.jar] 328 [Loaded java.io.FilePermission from C:\jdk1.6.0\jre\lib\rt.jar] 329 [Loaded java.io.FilePermission$1 from C:\jdk1.6.0\jre\lib\rt.jar] 330 [Loaded java.security.Policy from C:\jdk1.6.0\jre\lib\rt.jar] 331 [Loaded sun.security.provider.PolicyFile from C:\jdk1.6.0\jre\lib\rt.jar] 332 [Loaded java.security.Policy$UnsupportedEmptyCollection from C:\jdk1.6.0\jre\lib\rt.jar] 333 [Loaded java.io.FilePermissionCollection from C:\jdk1.6.0\jre\lib\rt.jar] 334 [Loaded java.security.AllPermission from C:\jdk1.6.0\jre\lib\rt.jar] 335 [Loaded java.security.UnresolvedPermission from C:\jdk1.6.0\jre\lib\rt.jar] 336 [Loaded java.security.BasicPermissionCollection from C:\jdk1.6.0\jre\lib\rt.jar] 337 [Loaded sun.misc.JavaSecurityProtectionDomainAccess from C:\jdk1.6.0\jre\lib\rt.jar] 338 [Loaded java.security.ProtectionDomain$2 from C:\jdk1.6.0\jre\lib\rt.jar] 339 [Loaded java.security.ProtectionDomain$Key from C:\jdk1.6.0\jre\lib\rt.jar] 340 [Loaded java.security.Principal from C:\jdk1.6.0\jre\lib\rt.jar] 341 [Loaded Example from file:/D:/Users/wuxiong.wx/workspace/Test/LoadClass/Example/bin/] 342 343 准备实例化Cat 344 [Loaded Animal from file:/D:/Users/wuxiong.wx/workspace/Test/LoadClass/Example/bin/] 345 [Loaded Cat from file:/D:/Users/wuxiong.wx/workspace/Test/LoadClass/Example/bin/] 346 I am an animal 347 I am a cat 348 Cat实例化完成 349 350 准备加载MyTest类 351 第一次调用Class.forname() 352 [Loaded MyTest from file:/D:/Users/wuxiong.wx/workspace/Test/LoadClass/Example/bin/] 353 第二次调用Class.forname() 354 355 准备加载MyTest2类 356 [Loaded MyTest2 from file:/D:/Users/wuxiong.wx/workspace/Test/LoadClass/Example/bin/] 357 MyTest2的静态部分初始化 358 359 准备加载MyTest3类 360 [Loaded MyTest3 from file:/D:/Users/wuxiong.wx/workspace/Test/LoadClass/Example/bin/] 361 第二次调用ClassLoader.loadClass() 362 363 程序结束 364 365 [Loaded java.lang.Shutdown from C:\jdk1.6.0\jre\lib\rt.jar] 366 [Loaded java.lang.Shutdown$Lock from C:\jdk1.6.0\jre\lib\rt.jar]
2、例2:显示加载类
Class ClassLoader
- java.lang.Object
-
- java.lang.ClassLoader
- Direct Known Subclasses:
- SecureClassLoader
public abstract class ClassLoader extends Object
A class loader is an object that is responsible for loading classes. The class ClassLoader is an abstract class. Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system.
-
loadClass
public Class<?> loadClass(String name) throws ClassNotFoundException
Loads the class with the specified binary name. This method searches for classes in the same manner as theloadClass(String, boolean)
method. It is invoked by the Java virtual machine to resolve class references. Invoking this method is equivalent to invokingloadClass(name, false)
.- Parameters:
name
- The binary name of the class- Returns:
- The resulting Class object
- Throws:
ClassNotFoundException
- If the class was not found
Class Class<T>
- java.lang.Object
-
- java.lang.Class<T>
- Type Parameters:
T
- the type of the class modeled by thisClass
object. For example, the type ofString.class
isClass<String>
. UseClass<?>
if the class being modeled is unknown.
- All Implemented Interfaces:
- Serializable, AnnotatedElement, GenericDeclaration, Type
public final class Class<T> extends Object implements Serializable, GenericDeclaration, Type, AnnotatedElement
Instances of the classClass
represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as aClass
object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean
,byte
,char
,short
,int
,long
,float
, anddouble
), and the keywordvoid
are also represented asClass
objects.Class
has no public constructor. InsteadClass
objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to thedefineClass
method in the class loader.The following example uses a
Class
object to print the class name of an object:void printClassName(Object obj) { System.out.println("The class of " + obj + " is " + obj.getClass().getName()); }
It is also possible to get the
Class
object for a named type (or for void) using a class literal. See Section 15.8.2 of The Java™ Language Specification. For example:System.out.println("The name of class Foo is: "+Foo.class.getName());
- Since:
- JDK1.0
- See Also:
ClassLoader.defineClass(byte[], int, int)
, Serialized Form
-
forName
public static Class<?> forName(String className) throws ClassNotFoundException
Returns theClass
object associated with the class or interface with the given string name. Invoking this method is equivalent to:
whereClass.forName(className, true, currentLoader)
currentLoader
denotes the defining class loader of the current class.For example, the following code fragment returns the runtime
Class
descriptor for the class namedjava.lang.Thread
:Class t = Class.forName("java.lang.Thread")
A call to
forName("X")
causes the class namedX
to be initialized.- Parameters:
className
- the fully qualified name of the desired class.- Returns:
- the
Class
object for the class with the specified name. - Throws:
LinkageError
- if the linkage failsExceptionInInitializerError
- if the initialization provoked by this method failsClassNotFoundException
- if the class cannot be located