品易云推流 关闭
文章详情页
文章 > java基础 > java需要class是什么意思

java需要class是什么意思

java class

头像

小妮浅浅

2021-02-07 17:47:467777浏览 · 0收藏 · 0评论

本教程操作环境:windows7系统、java10版,DELL G3电脑。

1.概念

Class 类是在Java语言中定义一个特定类的实现,在java.lang 包中。一个类的定义包含成员变量,成员方法,还有这个类实现的接口,以及这个类的父类。Class类的对象用于表示当前运行的 Java 应用程序中的类和接口。

比如:每个数组均属于一个 Class 类对象,所有具有相同元素类型和维数的数组共享一个Class 对象。基本的 Java 类型(boolean, byte, char, short,int, long, float 和 double) 和 void 类型也可表示为 Class 对象。

2.特征

class类没有公有的构造方法,它由JVM自动调用(在new对象或者加载-classLoader时)。

下面的方法作用是打印出对象的class name:

void printClassName(Object obj) {
System.out.println("The class of " + obj +
" is " + obj.getClass().getName());
}

同样可以根据class literal 获得class name:

System.out.println("The name of class Foo is: "+Foo.class.getName());//你可以将Foo改为void尝试下。

3.使用实例

public final class Class<T> implements java.io.Serializable,GenericDeclaration,Type, AnnotatedElement {
    private static final int ANNOTATION= 0x00002000;
    private static final int ENUM      = 0x00004000;
    private static final int SYNTHETIC = 0x00001000;
 
    private static native void registerNatives();
    static {
        registerNatives();
    }
 
    /*
     * Private constructor. Only the Java Virtual Machine creates Class objects.(私有构造,只能由JVM创建该类)
     * This constructor is not used and prevents the default constructor being
     * generated.
     */
    private Class(ClassLoader loader) {
        // Initialize final field for classLoader.  The initialization value of non-null
        // prevents future JIT optimizations from assuming this final field is null.
        classLoader = loader;
}

以上就是关于java中需要class的讲解,可以说作为最基础的类,是所有类的共同属性。在实例的使用时,同样是不可缺少的存在,相信经过本篇的学习,大家已经能够明白class的重要性。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图