在 Java 中创建对象的所有不同方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/95419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are all the different ways to create an object in Java?
提问by Mike Deck
Had a conversation with a coworker the other day about this.
前几天和同事讨论了这个问题。
There's the obvious using a constructor, but what are the other ways there?
使用构造函数很明显,但其他方法是什么?
采纳答案by kamaci
There are four different ways to create objects in java:
在java中有四种不同的方式来创建对象:
A. Using new
keyword
This is the most common way to create an object in java. Almost 99% of objects are created in this way.
一个。使用new
关键字
这是在 Java 中创建对象最常见的方式。几乎 99% 的对象都是以这种方式创建的。
MyObject object = new MyObject();
B. Using Class.forName()
If we know the name of the class & if it has a public default constructor we can create an object in this way.
乙。使用Class.forName()
如果我们知道类的名称并且它有一个公共默认构造函数,我们可以通过这种方式创建一个对象。
MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();
C. Using clone()
The clone() can be used to create a copy of an existing object.
Ç。使用clone()
clone() 可用于创建现有对象的副本。
MyObject anotherObject = new MyObject();
MyObject object = (MyObject) anotherObject.clone();
D. Using object deserialization
Object deserialization is nothing but creating an object from its serialized form.
d。使用object deserialization
对象反序列化只不过是从其序列化形式创建一个对象。
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
You can read them from here.
您可以从这里阅读它们。
回答by Bill the Lizard
回答by John Meagher
Reflection:
反射:
someClass.newInstance();
回答by Garth Gilmour
Depends exactly what you mean by create but some other ones are:
完全取决于您所说的创建,但其他一些是:
- Clone method
- Deserialization
- Reflection (Class.newInstance())
- Reflection (Constructor object)
- 克隆方法
- 反序列化
- 反射(Class.newInstance())
- 反射(构造函数对象)
回答by Confusion
Within the Java language, the only way to create an object is by calling its constructor, be it explicitly or implicitly. Using reflection results in a call to the constructor method, deserialization uses reflection to call the constructor, factory methods wrap the call to the constructor to abstract the actual construction and cloning is similarly a wrapped constructor call.
在 Java 语言中,创建对象的唯一方法是调用其构造函数,无论是显式还是隐式。使用反射导致调用构造函数方法,反序列化使用反射调用构造函数,工厂方法包装对构造函数的调用以抽象实际构造,克隆类似地包装构造函数调用。
回答by Fabian Steeg
From an API user perspective, another alternative to constructors are static factory methods (like BigInteger.valueOf()), though for the API author (and technically "for real") the objects are still created using a constructor.
从 API 用户的角度来看,构造函数的另一种替代方法是静态工厂方法(如 BigInteger.valueOf()),但对于 API 作者(并且在技术上是“真实的”),对象仍然是使用构造函数创建的。
回答by Randy L
there is also ClassLoader.loadClass(string) but this is not often used.
还有 ClassLoader.loadClass(string) 但这并不经常使用。
and if you want to be a total lawyer about it, arrays are technicallyobjects because of an array's .length property. so initializing an array creates an object.
如果你想成为一个全面的律师,数组在技术上是对象,因为数组的 .length 属性。所以初始化一个数组会创建一个对象。
回答by Thomas L?tzer
Yes, you can create objects using reflection. For example, String.class.newInstance()
will give you a new empty String object.
是的,您可以使用反射创建对象。例如,String.class.newInstance()
会给你一个新的空 String 对象。
回答by Vincent Ramdhanie
Also you can use
你也可以使用
Object myObj = Class.forName("your.cClass").newInstance();
回答by ryanprayogo
Reflection will also do the job for you.
反思也会为你完成这项工作。
SomeClass anObj = SomeClass.class.newInstance();
is another way to create a new instance of a class. In this case, you will also need to handle the exceptions that might get thrown.
是另一种创建类的新实例的方法。在这种情况下,您还需要处理可能抛出的异常。
回答by stacker
This should be noticed if you are new to java, every object has inherited from Object
如果您是 Java 新手,应该注意这一点,每个对象都继承自 Object
protected native Object clone() throws CloneNotSupportedException;
受保护的本机对象 clone() 抛出 CloneNotSupportedException;