简体   繁体   English

构造函数是在 JAVA 中创建 class 的 object 的唯一方法吗?

[英]Is constructor the only way to create the object of a class in JAVA?

If a constructor is the only way to create the object of a class then how String name = "Java";如果构造函数是创建 object 的唯一方法 class 那么如何 String name = "Java"; is able to create an object of String class even without using constructor.即使不使用构造函数,也能够创建字符串 class 的 object。

No. Constructor is not the only way.不,构造函数不是唯一的方法。

There are at least two more ways:至少还有两种方式:

  1. Clone the object克隆 object
  2. Serialize and then deserialize object. 序列化然后反序列化object。

Though in case with your example - neither of these is used.尽管以您的示例为例-这些都没有使用。

In this case Java uses string pool在这种情况下 Java 使用字符串池

There is another way of creating objects via还有另一种创建对象的方法

  • Class.forName("fully.qualified.class.name.here").newInstance()

  • Class.forName("fully.qualified.class.name.here").getConstuctor().newInstance()

but they call constructor under the hood.但他们在幕后调用构造函数。

Other ways to create objects are cloning via clone() method and deserialization.创建对象的其他方法是通过clone()方法进行克隆和反序列化。

I suppose in a loop-hole type of way you could use the class object too:我想以一种漏洞的方式,您也可以使用 class object:

// Get the class object using an object you already have   
Class<?> clazz = object.getClass();

// or get class object using the type
Class<?> clazz = Object.class;

// Get the constructor object (give arguments 
// of Class objects of types if the constructor takes arguments)
Constructor<?> constructor = clazz.getConstructor();

// then invoke it (and pass arguments if need be)
Object o = constructor.newInstance();

I mean you still use the constructor so it probably doesn't really count.我的意思是你仍然使用构造函数,所以它可能不算数。 But hey, its there!但是,嘿,它就在那里!

Java doc link for Class object Java Class object 的文档链接

YES, each time a new object is created, at least one constructor will be invoked.是的,每次创建一个新的 object 时,都会调用至少一个构造函数。

Look at this tutorial , this will explain all with objects, classes and constructors.看看这个教程,这将解释所有的对象、类和构造函数。

暂无
暂无

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

相关问题 如何仅使用参数化构造函数为类创建对象? - how to create object for a class with only parameterized constructor? 在 Java 中,是否可以在没有 class 和构造函数的情况下创建 object? - In Java, Is it possible to create an object without a class and a constructor? 创建一个Java类的对象,该对象的构造函数以@Autowired注释 - Create an object of a java class whose constructor is annotated with @Autowired Java对象类,构造函数链接 - Java Object Class, Constructor Chaining Java:当java.io具有受保护的构造函数时,如何从java.io为Reader类创建新的类对象 - Java: How to create new class object for Reader class from java.io when it has protected constructor PHP-如果我们在php中创建子类的对象,它会调用父类构造函数,然后调用子类构造函数(如java)吗? - PHP -If we create object of child class in php, does it call parent class constructor and then child class constructor like java? 有没有一种优雅的方法可以根据用于创建该对象的构造函数来更改类的对象可以使用的方法/变量? - Is there an elegant way to change what methods/variables an object of a class can use based on the constructor used to create this object? Java-如何将对象创建限制为仅使用另一个类创建它 - Java - How to limit the object creation to only create it using another class 如何仅使用特定字段创建新的 java object class? - How I create new java object class with only certain field? Redisson中仅具有参数化构造函数的类的对象反序列化 - Object deserializtion in Redisson for a class with only parameterized constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM