简体   繁体   English

为什么类实例本身没有公共构造函数?

[英]Why class instance itself has no public constructor?

While reading Reflection API i got this "Every Java Type has a class Instance And the class instance itself has no public Constructor" .在阅读反射 API 时,我得到了这个“每个 Java 类型都有一个类实例而类实例本身没有公共构造函数”

Now this is really confusing for me.现在这对我来说真的很困惑。 Because what i have read till now says .因为我到目前为止所读到的内容。 All classes have constructor even when we don't specify any, we get constructor by default (even if the class is static) once we create its instance.即使我们没有指定任何类,所有类都有构造函数,一旦我们创建了它的实例,我们就会默认获得构造函数(即使类是静态的)。 Can someone explain me this in simple word有人可以用简单的词来解释我吗

(to show even static class has constructor) (显示甚至静态类具有构造函数)

public class Reader1 {
    private int pageNumber;

    private class ReaderName1{
    public int getPage(){
        return pageNumber;
    }
    }
    static class ReaderFound{

    }
} 

using javap使用 javap

class Reader1$ReaderFound {
          Reader1$ReaderFound();
        }

It says: no public constructor .它说:没有public构造函数 That doesn't mean it has no constructor at all.这并不意味着它根本没有构造函数。 It does have a private constructor, which you can also create, as below given class, which also doesn't have a public constructor.它确实有一个private构造函数,您也可以创建它,如下面给定的类,它也没有public构造函数。

class Test {
    private Test() { }
}

“每个 Java Type 都有一个类 Instance 而类实例本身没有公共构造函数”这意味着 java.lang.Class 没有公共构造函数,因为只有 JVM 可以创建 java.lang.Class 实例,并且每个类 Java Type 都有一个 java.lang.Class 实例。我认为这就是你感到困惑的原因。

In Java you can suppress a public constructor by defining it private in the inheriting class - for Class this means:在 Java 中,您可以通过在继承类中将其定义为private来抑制public构造函数 - 对于Class这意味着:

public final class Class<T> {
  private Class() {
    //this constructor is private, no other constructor exists or will be generated
  }
}

To create an instance of class there exist some factory methods, like Class.forName .要创建类的实例,存在一些工厂方法,例如Class.forName

Directly cited from the official website.直接从官网引用。 I hope it can help you.我希望它能帮助你。

The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.原始 Java 类型(boolean、byte、char、short、int、long、float 和 double)和关键字 void 也表示为 Class 对象。 Class has no public constructor .类没有公共构造函数 Instead a Class object is constructed automatically by the Java Virtual Machine when a class loader invokes one of the defineClass methods and passes the bytes of a class file.相反,当类加载器调用defineClass 方法之一并传递类文件的字节时,Java 虚拟机会自动构造一个Class 对象。

https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当一个类的实例本身被创建时,为什么构造函数中的语句没有被执行? - When an instance of a class is created in itself, then why the statements in the constructor are not executed? AssertionFailedError:<class>没有公共构造函数 - AssertionFailedError: <class> has no public constructor 为什么要让一个类创建自己的实例? - Why make a class create an instance of itself? 为什么类返回自身的实例? - Why does a class return an instance of itself? 为什么应该在javabean类中提供公共构造函数 - why public constructor should be provided in javabean class 为什么Calendar类没有公共构造函数? - Why does the Calendar class not have a public constructor? 如何使用具有私有构造函数的类的实例? - How to use an instance of a class that has a private constructor? 确保类名存在,是公共的,并且具有带有类名和空构造函数的公共片段的空构造函数 - make sure class name exists, is public, and has an empty constructor for public fragment with class name and empty constructor 嵌套类的公共构造函数 - Public constructor for nested class 是否可以将具有自身实例的类扩展为字段,以使子类具有该子类的实例呢? - Is it possible to extend a class that has an instance of itself as a field such that the subclass has an instance of the subclass instead?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM