java程序的运行过程中,所有的类和接口都会成为一个实例,即一个Class类的对象,所有的类(如enum)、接口(如annotation)、数组、基本数据类型和void类型都代表着Class对象。
Class没有公共的构造器,它是由JVM在加载类或者在类的加载器中调用方法时被自动构造出来的。
一、几点额外知识
enum:enum是一种数据类型(枚举类型)枚举类型定义的一般形式为:enum枚举名{ 枚举值表 };
Exp:
public class EnumDemo{
<span style="white-space:pre"> </span>private enum Seasons{
<span style="white-space:pre"> </span>winter,spring,summer,fall
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void main(String[]args){
<span style="white-space:pre"> </span>for(Seasons s:Seasons.values()){
<span style="white-space:pre"> </span>System.out.println(s);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
annotation: 提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Exp: @Deprecated
native关键字:A native method is a Java method whose implementation is provided by non-java code.
final关键字:不能被继承,锁定该方法
transient关键字:
1)一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。
2)transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。变量如果是用户自定义类变量,则该类需要实现Serializable接口。
3)被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。
volatile:程度较轻的 synchronized
二、对Class的初步认识
void printClassName(Object obj) {
System.out.println("The class of " + obj +" is " + obj.getClass().getName());
}
下面 简单介绍一下Class对象获取的三种方式:
三、Class的常用方法
public void exp() throws InstantiationException, IllegalAccessException{
String r;
r.getClass().newInstance();
}
3.isInstance() 与之相似的还有isInterface()等等
public class ClassDemo {
public static void main(String[] args) {
try {
Class cls = Class.forName("java.awt.Label");
System.out.println("Methods =");
Method m[] = cls.getMethods();
for(int i = 0; i < m.length; i++) {
System.out.println(m[i]);
}
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
10.Class中原子操作支持(源码,未仔细研究):
private static class Atomic {
// initialize Unsafe machinery here, since we need to call Class.class instance method
// and have to avoid calling it in the static initializer of the Class class...
private static final Unsafe unsafe = Unsafe.getUnsafe();
// offset of Class.reflectionData instance field
private static final long reflectionDataOffset;
// offset of Class.annotationType instance field
private static final long annotationTypeOffset;
static {
Field[] fields = Class.class.getDeclaredFields0(false); // bypass caches
reflectionDataOffset = objectFieldOffset(fields, "reflectionData");
annotationTypeOffset = objectFieldOffset(fields, "annotationType");
}
private static long objectFieldOffset(Field[] fields, String fieldName) {
Field field = searchFields(fields, fieldName);
if (field == null) {
throw new Error("No " + fieldName + " field found in java.lang.Class");
}
return unsafe.objectFieldOffset(field);
}
static <T> boolean casReflectionData(Class<?> clazz,
SoftReference<ReflectionData<T>> oldData,
SoftReference<ReflectionData<T>> newData) {
return unsafe.compareAndSwapObject(clazz, reflectionDataOffset, oldData, newData);
}
static <T> boolean casAnnotationType(Class<?> clazz,
AnnotationType oldType,
AnnotationType newType) {
return unsafe.compareAndSwapObject(clazz, annotationTypeOffset, oldType, newType);
}
}
四、一些小细节
1.getSimpleName()中: while (index < length && isAsciiDigit(simpleName.charAt(index)))
index++;
// Eventually, this is the empty string iff this is an anonymous class
return simpleName.substring(index);
index<length起到了防止charAt溢出的作用。
while (index < length && isAsciiDigit(simpleName.charAt(index)))
index++;
// Eventually, this is the empty string iff this is an anonymous class
return simpleName.substring(index);