第14章类型信息

1.类是程序的一部分,每个类都有一个Class对象。该Class对象被保存在一个同名的.class文件中。
2.Class对象包含了与类有关的信息,Class对象用于创建类的所有“常规”对象。

3.jvm通过类加载器这个子系统来加载要用到的类。

4.所有的类都是在对其第一次使用时,动态加载到jvm中的。当程序创建第一个对类的静态成员的引用时,就会加载这个类。这个证明构造器也是静态方法。因为,通过new创建新对象时,也会被当做对类的静态成员的引用。

5.类加载器首先检查这个类的Class对象是否已经加载。如果尚未加载,默认的类加载器就会根据类名查找.class文件。一旦类的class对象被载入内存,它就用来创建这个类的所有对象。

6.static子句,是在类第一次被加载时执行。即,static初始化是在类加载时执行的。


7.java还提供了另一种方法来生成对class对象的引用,即类字面常量。例如
FancyToy.class
这个方法更安全、更高效、更简单,建议使用这种方式。它不仅适用于普通类,还适用于接口、数组和基本类型。基本类型的包装器,还有一个标准字段TYPE,指向对应基本类型的class对象。为了保持统一,建议还是使用.class吧


8.为了使用类而做的准备工作实际包含3个步骤:
(1).加载,通过类加载器寻找.class文件并加载;
(2).链接,为静态域分配存储空间;
(3).初始化,执行静态初始化器和静态初始化块;

可见,初始化被延迟到了对静态方法(构造器隐式地为静态的)或者非常数静态域进行首次引用时才执行。


9.仅使用.class语法来获取对类的引用不会引发初始化。但是,Class.forName()立即就进行了初始化。如果一个static final值是编译期常量,那么这个值不需要对该类进行初始化就可以读到,例如:

static final int staticFinal = 47;
如果只是static final而不是编译期常量,那么在读取的时候,就要进行初始化。例如:
static final int staticFinal2 = ClassInitialization.rand.nextInt(1000);

如果一个static域不是final的,那么在访问它时,总要进行链接(分配存储空间)和初始化(初始化该存储空间)。


附注:Class对象的一些主要方法用处
1.getInterfaces

public Class[] getInterfaces()

Determines the interfaces implemented by the class or interface represented by this object.

If this object represents a class, the return value is an array containing objects representing all interfaces implemented by the

class. The order of the interface objects in the array corresponds to the order of the interface names in the implements clause of the

declaration of the class represented by this object. For example, given the declaration:

class Shimmer implements FloorWax, DessertTopping { ... }


suppose the value of s is an instance of Shimmer; the value of the expression:

s.getClass().getInterfaces()[0]


is the Class object that represents interface FloorWax; and the value of:

s.getClass().getInterfaces()[1]


is the Class object that represents interface DessertTopping.

If this object represents an interface, the array contains objects representing all interfaces extended by the interface. The order

of the interface objects in the array corresponds to the order of the interface names in the extends clause of the declaration of the

interface represented by this object.

If this object represents a class or interface that implements no interfaces, the method returns an array of length 0.

If this object represents a primitive type or void, the method returns an array of length 0.

Returns:
an array of interfaces implemented by this class.

2.getSuperclass

public Class<? super T> getSuperclass()

Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class. If

this Class represents either the Object class, an interface, a primitive type, or void, then null is returned. If this object represents

an array class then the Class object representing the Object class is returned.

Returns:
the superclass of the class represented by this object.

3.newInstance //创建所表示类的一个实例 ,当时所表示的类,必须有默认的构造函数

public T newInstance()
throws InstantiationException,
IllegalAccessException

Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an

empty argument list. The class is initialized if it has not already been initialized.

Note that this method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method

effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor.newInstance

method avoids this problem by wrapping any exception thrown by the constructor in a (checked) InvocationTargetException.

Returns:
a newly allocated instance of the class represented by this object.
4.getCanonicalName

public String getCanonicalName()

Returns the canonical name of the the underlying class as defined by the Java Language Specification. Returns null if the underlying

class does not have a canonical name (i.e., if it is a local or anonymous class or an array whose component type does not have a

canonical name).

Returns:
the canonical name of the underlying class if it exists, and null otherwise.
Since:
1.5

5.forName //必须带上包名称,如果已经有一个类的对象了,可以通过getClass()方法来获取相应的class对象。

public static Class<?> forName(String className)
throws ClassNotFoundException

Returns the Class object associated with the class or interface with the given string name.
Parameters:
className - the fully qualified name of the desired class.
Returns:
the Class object for the class with the specified name.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值