黑马程序员:我对 反射机制 的理解

---------------------- android培训、java培训、期待与您交流! ----------------------

  1. JAVA程序的运行ClassLoaderclass文件Load到内存中的code segmentJVM找到main方法开始执行,而且执行的过程中会有更多的class文件被Load进内存(动态加载:并非一次性加载,需要时加载)!

public class Testext {
  public static void main(String[] args) {
  new Fu();
  System.out.println("-----helpless seperator line-----");
                                           new Zi();
  }
}

class Fu {}

class Zi {}

 

运行时设定VM arguments-verbose:class输出结果如下:

[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
[Loaded java.lang.reflect.AnnotatedElement from shared objects file]
[Loaded java.lang.Class from shared objects file]
[Loaded java.lang.Cloneable from shared objects file]
[Loaded java.lang.ClassLoader from shared objects file]
[Loaded java.lang.System from shared objects file]
[Loaded java.lang.Throwable from shared objects file]
[Loaded java.lang.Error from shared objects file]
[Loaded java.lang.ThreadDeath from shared objects file]
[Loaded java.lang.Exception from shared objects file]
[Loaded java.lang.RuntimeException from shared objects file]
[Loaded java.security.ProtectionDomain from shared objects file]
[Loaded java.security.AccessControlContext from shared objects file]
[Loaded java.lang.ReflectiveOperationException from shared objects file]
[Loaded java.lang.ClassNotFoundException from shared objects file]
[Loaded java.lang.LinkageError from shared objects file]
[Loaded java.lang.NoClassDefFoundError from shared objects file]
[Loaded java.lang.ClassCastException from shared objects file]
[Loaded java.lang.ArrayStoreException from shared objects file]
[Loaded java.lang.VirtualMachineError from shared objects file]
[Loaded java.lang.OutOfMemoryError from shared objects file]
[Loaded java.lang.StackOverflowError from shared objects file]
[Loaded java.lang.IllegalMonitorStateException from shared objects file]
[Loaded java.lang.ref.Reference from shared objects file]
[Loaded java.lang.ref.SoftReference from shared objects file]
[Loaded java.lang.ref.WeakReference from shared objects file]
[Loaded java.lang.ref.FinalReference from shared objects file]
[Loaded java.lang.ref.PhantomReference from shared objects file]
[Loaded java.lang.ref.Finalizer from shared objects file]
[Loaded java.lang.Runnable from shared objects file]
[Loaded java.lang.Thread from shared objects file]
[Loaded java.lang.Thread$UncaughtExceptionHandler from shared objects file]
[Loaded java.lang.ThreadGroup from shared objects file]
[Loaded java.util.Dictionary from shared objects file]
[Loaded java.util.Map from shared objects file]
[Loaded java.util.Hashtable from shared objects file]
[Loaded java.util.Properties from shared objects file]
[Loaded java.lang.reflect.AccessibleObject from shared objects file]
[Loaded java.lang.reflect.Member from shared objects file]
[Loaded java.lang.reflect.Field from shared objects file]
[Loaded java.lang.reflect.Method from shared objects file]
[Loaded java.lang.reflect.Constructor from shared objects file]
[Loaded sun.reflect.MagicAccessorImpl from shared objects file]
[Loaded sun.reflect.MethodAccessor from shared objects file]
[Loaded sun.reflect.MethodAccessorImpl from shared objects file]
[Loaded sun.reflect.ConstructorAccessor from shared objects file]
[Loaded sun.reflect.ConstructorAccessorImpl from shared objects file]
[Loaded sun.reflect.DelegatingClassLoader from shared objects file]
[Loaded sun.reflect.ConstantPool from shared objects file]
[Loaded sun.reflect.FieldAccessor from shared objects file]
[Loaded sun.reflect.FieldAccessorImpl from shared objects file]
[Loaded sun.reflect.UnsafeFieldAccessorImpl from shared objects file]
[Loaded sun.reflect.UnsafeStaticFieldAccessorImpl from shared objects file]
[Loaded java.lang.invoke.MethodHandle from shared objects file]
[Loaded java.lang.invoke.MemberName from shared objects file]
[Loaded java.lang.invoke.MethodHandleNatives from shared objects file]
[Loaded java.lang.invoke.BoundMethodHandle from shared objects file]
[Loaded java.lang.invoke.AdapterMethodHandle from shared objects file]
[Loaded java.lang.invoke.DirectMethodHandle from shared objects file]
[Loaded java.lang.invoke.MethodType from shared objects file]
[Loaded java.lang.invoke.MethodTypeForm from shared objects file]
[Loaded java.lang.BootstrapMethodError from shared objects file]
[Loaded java.lang.invoke.WrongMethodTypeException from shared objects file]
[Loaded java.lang.invoke.CallSite from shared objects file]
[Loaded java.lang.invoke.CountingMethodHandle from shared objects file]
[Loaded java.lang.invoke.ConstantCallSite from shared objects file]
[Loaded java.lang.invoke.MutableCallSite from shared objects file]
[Loaded java.lang.invoke.VolatileCallSite from shared objects file]
[Loaded java.lang.Appendable from shared objects file]
[Loaded java.lang.AbstractStringBuilder from shared objects file]
[Loaded java.lang.StringBuffer from shared objects file]
[Loaded java.lang.StringBuilder from shared objects file]
[Loaded java.lang.StackTraceElement from shared objects file]
[Loaded java.nio.Buffer from shared objects file]
[Opened D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.PostVMInitHook from shared objects file]
[Loaded java.lang.Boolean from shared objects file]
[Loaded java.lang.Character from shared objects file]
[Loaded java.lang.Number from shared objects file]
[Loaded java.lang.Float from shared objects file]
[Loaded java.lang.Double from shared objects file]
[Loaded java.lang.Byte from shared objects file]
[Loaded java.lang.Short from shared objects file]
[Loaded java.lang.Integer from shared objects file]
[Loaded java.lang.Long from shared objects file]
[Loaded java.io.ObjectStreamField from shared objects file]
[Loaded java.util.Comparator from shared objects file]
[Loaded java.lang.String$CaseInsensitiveComparator from shared objects file]
[Loaded java.security.Guard from shared objects file]
[Loaded java.security.Permission from shared objects file]
[Loaded java.security.BasicPermission from shared objects file]
[Loaded java.lang.RuntimePermission from shared objects file]
[Loaded java.security.AccessController from shared objects file]
[Loaded java.lang.reflect.ReflectPermission from shared objects file]
[Loaded java.security.PrivilegedAction from shared objects file]
[Loaded sun.reflect.ReflectionFactory$GetReflectionFactoryAction from shared objects file]
[Loaded java.security.cert.Certificate from shared objects file]
[Loaded java.lang.Iterable from shared objects file]
[Loaded java.util.Collection from shared objects file]
[Loaded java.util.AbstractCollection from shared objects file]
[Loaded java.util.List from shared objects file]
[Loaded java.util.AbstractList from shared objects file]
[Loaded java.util.RandomAccess from shared objects file]
[Loaded java.util.Vector from shared objects file]
[Loaded java.util.Stack from shared objects file]
[Loaded sun.reflect.ReflectionFactory from shared objects file]
[Loaded java.lang.ref.Reference$Lock from shared objects file]
[Loaded java.lang.ref.Reference$ReferenceHandler from shared objects file]
[Loaded java.lang.ref.ReferenceQueue from shared objects file]
[Loaded java.lang.ref.ReferenceQueue$Null from shared objects file]
[Loaded java.lang.ref.ReferenceQueue$Lock from shared objects file]
[Loaded java.lang.ref.Finalizer$FinalizerThread from shared objects file]
[Loaded java.util.Map$Entry from shared objects file]
[Loaded java.util.Hashtable$Entry from shared objects file]
[Loaded java.nio.charset.Charset from shared objects file]
[Loaded java.nio.charset.spi.CharsetProvider from shared objects file]
[Loaded sun.nio.cs.FastCharsetProvider from shared objects file]
[Loaded sun.nio.cs.StandardCharsets from shared objects file]
[Loaded java.util.AbstractMap from shared objects file]
[Loaded sun.util.PreHashedMap from shared objects file]
[Loaded sun.nio.cs.StandardCharsets$Aliases from shared objects file]
[Loaded sun.nio.cs.StandardCharsets$Classes from shared objects file]
[Loaded sun.nio.cs.StandardCharsets$Cache from shared objects file]
[Loaded java.lang.ThreadLocal from shared objects file]
[Loaded java.util.concurrent.atomic.AtomicInteger from shared objects file]
[Loaded sun.misc.Unsafe from shared objects file]
[Loaded java.lang.IncompatibleClassChangeError from shared objects file]
[Loaded java.lang.NoSuchMethodError from shared objects file]
[Loaded java.util.ArrayList from shared objects file]
[Loaded java.util.Collections from shared objects file]
[Loaded java.util.Set from shared objects file]
[Loaded java.util.AbstractSet from shared objects file]
[Loaded java.util.Collections$EmptySet from shared objects file]
[Loaded java.util.Collections$EmptyList from shared objects file]
[Loaded java.util.Collections$EmptyMap from shared objects file]
[Loaded java.util.Collections$UnmodifiableCollection from shared objects file]
[Loaded java.util.Collections$UnmodifiableList from shared objects file]
[Loaded java.util.Collections$UnmodifiableRandomAccessList from shared objects file]
[Loaded sun.reflect.Reflection from shared objects file]
[Loaded java.util.HashMap from shared objects file]
[Loaded java.util.HashMap$Entry from shared objects file]
[Loaded java.lang.Math from shared objects file]
[Loaded java.util.HashMap$EntrySet from shared objects file]
[Loaded java.util.Iterator from shared objects file]
[Loaded java.util.HashMap$HashIterator from shared objects file]
[Loaded java.util.HashMap$EntryIterator from shared objects file]
[Loaded java.lang.Class$3 from shared objects file]
[Loaded java.lang.reflect.Modifier from shared objects file]
[Loaded sun.reflect.LangReflectAccess from shared objects file]
[Loaded java.lang.reflect.ReflectAccess from shared objects file]
[Loaded java.util.Arrays from shared objects file]
[Loaded java.nio.charset.Charset$3 from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Opened D:\Soft\Program\JAVA\JDK\jre\lib\charsets.jar]
[Loaded sun.nio.cs.AbstractCharsetProvider from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.nio.cs.ext.ExtendedCharsets from D:\Soft\Program\JAVA\JDK\jre\lib\charsets.jar]
[Loaded java.lang.Class$1 from shared objects file]
[Loaded sun.reflect.ReflectionFactory$1 from shared objects file]
[Loaded sun.reflect.NativeConstructorAccessorImpl from shared objects file]
[Loaded sun.reflect.DelegatingConstructorAccessorImpl from shared objects file]
[Loaded java.util.SortedMap from shared objects file]
[Loaded java.util.NavigableMap from shared objects file]
[Loaded java.util.TreeMap from shared objects file]
[Loaded sun.misc.ASCIICaseInsensitiveComparator from shared objects file]
[Loaded java.util.TreeMap$Entry from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.VM from shared objects file]
[Loaded sun.nio.cs.HistoricallyNamedCharset from shared objects file]
[Loaded sun.nio.cs.ext.GBK from D:\Soft\Program\JAVA\JDK\jre\lib\charsets.jar]
[Loaded java.lang.StringCoding from shared objects file]
[Loaded java.lang.ThreadLocal$ThreadLocalMap from shared objects file]
[Loaded java.lang.ThreadLocal$ThreadLocalMap$Entry from shared objects file]
[Loaded java.lang.StringCoding$StringDecoder from shared objects file]
[Loaded sun.nio.cs.ext.DoubleByte from D:\Soft\Program\JAVA\JDK\jre\lib\charsets.jar]
[Loaded sun.nio.cs.ext.DelegatableDecoder from D:\Soft\Program\JAVA\JDK\jre\lib\charsets.jar]
[Loaded java.nio.charset.CharsetDecoder from shared objects file]
[Loaded sun.nio.cs.ext.DoubleByte$Decoder from D:\Soft\Program\JAVA\JDK\jre\lib\charsets.jar]
[Loaded java.nio.charset.CodingErrorAction from shared objects file]
[Loaded sun.nio.cs.ArrayDecoder from shared objects file]
[Loaded java.nio.ByteBuffer from shared objects file]
[Loaded java.nio.HeapByteBuffer from shared objects file]
[Loaded java.nio.Bits from shared objects file]
[Loaded java.nio.ByteOrder from shared objects file]
[Loaded sun.misc.JavaNioAccess from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.nio.Bits$1 from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.SharedSecrets from shared objects file]
[Loaded java.lang.Readable from shared objects file]
[Loaded java.nio.CharBuffer from shared objects file]
[Loaded java.nio.HeapCharBuffer from shared objects file]
[Loaded java.nio.charset.CoderResult from shared objects file]
[Loaded java.nio.charset.CoderResult$Cache from shared objects file]
[Loaded java.nio.charset.CoderResult$1 from shared objects file]
[Loaded java.nio.charset.CoderResult$2 from shared objects file]
[Loaded java.util.Hashtable$EntrySet from shared objects file]
[Loaded java.util.Collections$SynchronizedCollection from shared objects file]
[Loaded java.util.Collections$SynchronizedSet from shared objects file]
[Loaded java.util.Enumeration from shared objects file]
[Loaded java.util.Hashtable$Enumerator from shared objects file]
[Loaded java.lang.Runtime from shared objects file]
[Loaded sun.misc.Version from shared objects file]
[Loaded java.lang.AutoCloseable from shared objects file]
[Loaded java.io.Closeable from shared objects file]
[Loaded java.io.InputStream from shared objects file]
[Loaded java.io.FileInputStream from shared objects file]
[Loaded java.io.FileDescriptor from shared objects file]
[Loaded sun.misc.JavaIOFileDescriptorAccess from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.io.FileDescriptor$1 from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.io.Flushable from shared objects file]
[Loaded java.io.OutputStream from shared objects file]
[Loaded java.io.FileOutputStream from shared objects file]
[Loaded java.io.FilterInputStream from shared objects file]
[Loaded java.io.BufferedInputStream from shared objects file]
[Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater from shared objects file]
[Loaded java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl from shared objects file]
[Loaded sun.reflect.misc.ReflectUtil from shared objects file]
[Loaded java.io.FilterOutputStream from shared objects file]
[Loaded java.io.PrintStream from shared objects file]
[Loaded java.io.BufferedOutputStream from shared objects file]
[Loaded java.io.Writer from shared objects file]
[Loaded java.io.OutputStreamWriter from shared objects file]
[Loaded sun.nio.cs.StreamEncoder from shared objects file]
[Loaded sun.security.action.GetPropertyAction from shared objects file]
[Loaded java.nio.charset.CharsetEncoder from shared objects file]
[Loaded sun.nio.cs.ext.DoubleByte$Encoder from D:\Soft\Program\JAVA\JDK\jre\lib\charsets.jar]
[Loaded java.io.BufferedWriter from shared objects file]
[Loaded java.io.File from shared objects file]
[Loaded java.io.FileSystem from shared objects file]
[Loaded java.io.Win32FileSystem from shared objects file]
[Loaded java.io.WinNTFileSystem from shared objects file]
[Loaded java.io.ExpiringCache from shared objects file]
[Loaded java.util.LinkedHashMap from shared objects file]
[Loaded java.io.ExpiringCache$1 from shared objects file]
[Loaded java.util.LinkedHashMap$Entry from shared objects file]
[Loaded java.lang.ClassLoader$3 from shared objects file]
[Loaded java.io.ExpiringCache$Entry from shared objects file]
[Loaded java.lang.ClassLoader$NativeLibrary from shared objects file]
[Loaded java.lang.StringCoding$StringEncoder from shared objects file]
[Loaded sun.nio.cs.ArrayEncoder from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.lang.Terminator from shared objects file]
[Loaded sun.misc.SignalHandler from shared objects file]
[Loaded java.lang.Terminator$1 from shared objects file]
[Loaded sun.misc.Signal from shared objects file]
[Loaded sun.misc.NativeSignalHandler from shared objects file]
[Loaded sun.misc.OSEnvironment from shared objects file]
[Loaded sun.io.Win32ErrorMode from shared objects file]
[Loaded sun.misc.JavaLangAccess from shared objects file]
[Loaded java.lang.System$2 from shared objects file]
[Loaded java.lang.NullPointerException from shared objects file]
[Loaded java.lang.ArithmeticException from shared objects file]
[Loaded java.lang.Compiler from shared objects file]
[Loaded java.lang.Compiler$1 from shared objects file]
[Loaded sun.misc.Launcher from shared objects file]
[Loaded java.net.URLStreamHandlerFactory from shared objects file]
[Loaded sun.misc.Launcher$Factory from shared objects file]
[Loaded java.security.SecureClassLoader from shared objects file]
[Loaded java.net.URLClassLoader from shared objects file]
[Loaded sun.misc.Launcher$ExtClassLoader from shared objects file]
[Loaded sun.security.util.Debug from shared objects file]
[Loaded java.lang.ClassLoader$ParallelLoaders from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.util.WeakHashMap from shared objects file]
[Loaded java.util.WeakHashMap$Entry from shared objects file]
[Loaded java.util.Collections$SetFromMap from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.util.WeakHashMap$KeySet from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.JavaNetAccess from shared objects file]
[Loaded java.net.URLClassLoader$7 from shared objects file]
[Loaded java.util.StringTokenizer from shared objects file]
[Loaded java.security.PrivilegedExceptionAction from shared objects file]
[Loaded sun.misc.Launcher$ExtClassLoader$1 from shared objects file]
[Loaded sun.misc.MetaIndex from shared objects file]
[Loaded java.io.Reader from shared objects file]
[Loaded java.io.BufferedReader from shared objects file]
[Loaded java.io.InputStreamReader from shared objects file]
[Loaded java.io.FileReader from shared objects file]
[Loaded sun.nio.cs.StreamDecoder from shared objects file]
[Loaded java.lang.reflect.Array from shared objects file]
[Loaded java.util.Locale from shared objects file]
[Loaded sun.util.locale.LocaleObjectCache from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.util.Locale$Cache from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.util.concurrent.ConcurrentMap from shared objects file]
[Loaded java.util.concurrent.ConcurrentHashMap from shared objects file]
[Loaded java.util.concurrent.ConcurrentHashMap$HashEntry from shared objects file]
[Loaded java.util.concurrent.locks.Lock from shared objects file]
[Loaded java.util.concurrent.locks.ReentrantLock from shared objects file]
[Loaded java.util.concurrent.ConcurrentHashMap$Segment from shared objects file]
[Loaded java.util.concurrent.locks.AbstractOwnableSynchronizer from shared objects file]
[Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer from shared objects file]
[Loaded java.util.concurrent.locks.ReentrantLock$Sync from shared objects file]
[Loaded java.util.concurrent.locks.ReentrantLock$NonfairSync from shared objects file]
[Loaded java.util.concurrent.locks.AbstractQueuedSynchronizer$Node from shared objects file]
[Loaded sun.util.locale.BaseLocale from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.util.locale.BaseLocale$Cache from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.util.locale.BaseLocale$Key from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.util.locale.LocaleObjectCache$CacheEntry from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.util.Locale$LocaleKey from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.util.locale.LocaleUtils from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.lang.CharacterData from shared objects file]
[Loaded java.lang.CharacterDataLatin1 from shared objects file]
[Loaded sun.net.www.ParseUtil from shared objects file]
[Loaded java.util.BitSet from shared objects file]
[Loaded java.net.URL from shared objects file]
[Loaded java.net.Parts from shared objects file]
[Loaded java.net.URLStreamHandler from shared objects file]
[Loaded sun.net.www.protocol.file.Handler from shared objects file]
[Loaded sun.misc.JavaSecurityAccess from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.security.ProtectionDomain$1 from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.JavaSecurityProtectionDomainAccess from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.security.ProtectionDomain$3 from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.security.CodeSource from shared objects file]
[Loaded java.security.ProtectionDomain$Key from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.security.Principal from shared objects file]
[Loaded java.util.HashSet from shared objects file]
[Loaded sun.misc.URLClassPath from shared objects file]
[Loaded sun.net.www.protocol.jar.Handler from shared objects file]
[Loaded sun.misc.Launcher$AppClassLoader from shared objects file]
[Loaded sun.misc.Launcher$AppClassLoader$1 from shared objects file]
[Loaded java.lang.SystemClassLoaderAction from shared objects file]
[Loaded java.lang.Enum from shared objects file]
[Loaded sun.launcher.LauncherHelper from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.net.URLClassLoader$1 from shared objects file]
[Loaded sun.net.util.URLUtil from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.URLClassPath$3 from shared objects file]
[Loaded sun.misc.URLClassPath$Loader from shared objects file]
[Loaded sun.misc.URLClassPath$JarLoader from shared objects file]
[Loaded java.security.PrivilegedActionException from shared objects file]
[Loaded sun.misc.URLClassPath$FileLoader from shared objects file]
[Loaded sun.misc.Resource from shared objects file]
[Loaded sun.misc.URLClassPath$FileLoader$1 from shared objects file]
[Loaded java.lang.Package from shared objects file]
[Loaded sun.nio.ByteBuffered from shared objects file]
[Loaded sun.misc.PerfCounter from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.Perf$GetPerfAction from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.Perf from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.misc.PerfCounter$CoreCounters from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded sun.nio.ch.DirectBuffer from shared objects file]
[Loaded java.nio.MappedByteBuffer from shared objects file]
[Loaded java.nio.DirectByteBuffer from shared objects file]
[Loaded java.nio.LongBuffer from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.nio.DirectLongBufferU from D:\Soft\Program\JAVA\JDK\jre\lib\rt.jar]
[Loaded java.security.PermissionCollection from shared objects file]
[Loaded java.security.Permissions from shared objects file]
[Loaded java.net.URLConnection from shared objects file]
[Loaded sun.net.www.URLConnection from shared objects file]
[Loaded sun.net.www.protocol.file.FileURLConnection from shared objects file]
[Loaded sun.net.www.MessageHeader from shared objects file]
[Loaded java.io.FilePermission from shared objects file]
[Loaded java.io.FilePermission$1 from shared objects file]
[Loaded java.io.FilePermissionCollection from shared objects file]
[Loaded java.security.AllPermission from shared objects file]
[Loaded java.security.UnresolvedPermission from shared objects file]
[Loaded java.security.BasicPermissionCollection from shared objects file]
[Loaded sttt.Testext from file:/D:/DTemp/JAVAWorkspace/suiusoar/bin/]
[Loaded java.lang.Void from shared objects file]
[Loaded sttt.Fu from file:/D:/DTemp/JAVAWorkspace/suiusoar/bin/]
-----helpless seperator line-----
[Loaded sttt.Zi from file:/D:/DTemp/JAVAWorkspace/suiusoar/bin/]
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]
可见:Fu.class先load进内存,程序运行期间,Zi.class又load进内存

 

静态语句块在程序被load进内存后首先执行且只执行一次:

public class Testext {
	public static void main(String[] args) {
		new Sun();
		new Sun();
	}
}

class Sun {
	static {
		System.out.println("静态语句块被执行一次!");
	}
}	//输出:静态语句块被执行一次!

动态语句块在每次new一个对象时被执行一次,相当于在WaiSun的每个构造函数中都执行的语句:

public class Testext {
	public static void main(String[] args) {
		new WaiSun();
		new WaiSun();
	}
}

class WaiSun {
	{
		System.out.println("动态态语句块被执行一次!");
	}
}	//输出:动态态语句块执行多次!动态态语句块执行多次!

ClassLoader的分类

bootstrap class loader:装载JDK最核心的类以及其它的ClassLoader,然后其它的ClassLoader再装载其它类!bootstrap class loader用操作系统语言实现,而其它 

的ClassLoader都是用java编写。

System.out.println(String.class.getClassLoader());

ExtClassLoader:装载目录jre\lib\ext目录下的类,

System.out.println(com.sun.crypto.provider.DESKeyFactory.class.getClassLoader().getClass().getName());

AppClassLoader:装载程序员自己写的类(ClassLoader.getSystemClassLoader())

System.out.println(Testext.class.getClassLoader().getClass().getName());

System.out.println(ClassLoader.getSystemClassLoader().getClass().getName());

其它ClassLoader:SecureClassLoader->URLClassLoader

各种ClassLoader的层次关系:(这里说的是bootstrap class loader、ExtClassLoader、AppClassLoader对象之间的关系,不是类与类间的关系,不是继承)

可以这样认为:其它ClassLoader包含有AppClassLoader的引用,而AppClassLoader中有一个ExtClassLoader的引用……这样就形成了层次关系,所以可以通过方法getParent()找到所有的ClassLoader

public class Testext {
	public static void main(String[] args) {
		ClassLoader cl = Testext.class.getClassLoader();
		while (null != cl) {
			System.out.println(cl.getClass().getName());
			cl = cl.getParent();
		}
	}
}

输出结果:

sun.misc.Launcher$AppClassLoader
sun.misc.Launcher$ExtClassLoader

JDK中类加载的原理:ClassLoader在load类时首先查看上一层的ClassLoader是否已经加载过了,若已经加载,则不会重新加载!

优点:安全性高

       2.  反射

反射的特点:在程序运行期间,动态加载一个类,动态new对象,动态了解对象内部结构,动态调用对象的方法。(可将类名写在配置文件中)

所以可以称JAVA是半动态化语言,真正动态化语言是JS、Python……

理解JDK中的反射的前提:对于ClassLoader来说,class文件、class中的方法、属性,都可以看作是对象。

class文件的类型:java.lang.Class   

方法的类型:java.lang.reflect.Method

属性的类型:java.lang.reflect.Field

知识点:invoke(Object obj,Object... args)可变参数的方法,obj后面可传0个或多个参数!

下面是测试程序:

public class Testext {
	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException,
			IllegalArgumentException, InvocationTargetException {
		String name = "sttt.Fu";
		Class c = Class.forName(name);	//根据方法名得到这个类
		Object o = c.newInstance();		//创建类的实例
		Method[] methods = c.getMethods();	//得到类中所有方法
		for (Method method : methods) {
			if ("mike".equals(method.getName())) {
				method.invoke(o);	//根据方法名调用无参数的构造方法
			}
			if ("sad".equals(method.getName())) {
				Class rt = method.getReturnType();
				System.out.println(rt.getName());	//根据方法名得到方法的返回值类型
			}
			if ("calm".equals(method.getName())) {
				method.invoke(o, 20, 180);	//根据方法名调用有参数的构造方法
				for (Class paraType : method.getParameterTypes()) {
					System.out.println(paraType.getName());	//根据方法得到方法中参数的类型
				}	//可以根据参数的个数及类型动态传入方法的参数
			}
		}

	}
}

class Fu {
	Fu() {}

	int age;
	int height;
	String name = "中国";

	public void mike() {
		System.out.println("i am mike");
	}

	public void happy(int age) {
		this.age = age;
	}

	public void calm(int age, int height) {
		this.age = age;
		this.height = height;
		System.out.println(this.age + "---" + this.height);
	}

	public String sad() {
		return name;
	}
}

注意:若想要得到类中的属性,则属性的修饰符必须是public

Field[] fields = c.getFields();

  for (Field field : fields) {
   System.out.println(field.getName());
  }

总结:反射机制是为了描述或使用运行期间类的元信息:类名称、方法、属性

 


 

 

 

---------------------- android培训、java培训、期待与您交流! ----------------------

 

 

详细请查看:http://edu.csdn.net/heima

你可能感兴趣的:(随便聊,file,classloader,jdk,java,class,string)