Java 调用 new 和 getInstance() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3170159/
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
Difference between calling new and getInstance()
提问by SIr Codealot
Is calling Class.getInstance()
equivalent to new Class()
?
I know the constructor is called for the latter, but what about getInstance()
?
Thanks.
呼叫Class.getInstance()
等价于new Class()
? 我知道构造函数是为后者调用的,但是呢getInstance()
?谢谢。
采纳答案by BalusC
There is no such method as Class#getInstance()
. You're probably confusing it with Class#newInstance()
. And yes, this does exactly the same as new
on the defaultconstructor. Here's an extract of its Javadoc:
没有这样的方法Class#getInstance()
。您可能将它与Class#newInstance()
. 是的,这new
与默认构造函数完全相同。这是其Javadoc的摘录:
Creates a new instance of the class represented by this
Class
object. The class is instantiated as if by anew
expression with an empty argument list.The class is initialized if it has not already been initialized.
创建此
Class
对象表示的类的新实例。类被实例化,就好像由一个new
带有空参数列表的表达式一样。如果该类尚未初始化,则该类已初始化。
In code,
在代码中,
Object instance = Object.class.newInstance();
is the same as
是相同的
Object instance = new Object();
The Class#newInstance()
call actually follows the Factory Methodpattern.
该Class#newInstance()
调用实际上遵循工厂方法模式。
Update: seeing the other answers, I realize that there's some ambiguity in your question. Well, places where a method actuallynamed getInstance()
is been used often denotes an Abstract Factorypattern. It will "under the hoods" use new
or Class#newInstance()
to create and return the instance of interest. It's just to hide all the details about the concrete implementations which you may not need to know about.
更新:看到其他答案,我意识到你的问题有些含糊不清。嗯,使用实际命名的方法的地方getInstance()
通常表示抽象工厂模式。它将“在幕后”使用new
或Class#newInstance()
创建并返回感兴趣的实例。这只是为了隐藏您可能不需要了解的有关具体实现的所有细节。
Further you also see this methodname often in some (mostly homegrown) implementations of the Singletonpattern.
此外,您还经常在单例模式的某些(主要是自产的)实现中看到这个方法名。
See also:
也可以看看:
回答by SIr Codealot
Absolutely (usually) not.
绝对(通常)不是。
getInstance
is the static method often used with the Singleton Patternin Java. The new
keywordactually creates a new object. At some point there must be a new
(although there are a few other methods to instantiate new objects) to actually create the object that getInstance
returns.
getInstance
是Java 中经常与单例模式一起使用的静态方法。该new
关键字实际上创建了一个新对象。在某些时候,必须有一个new
(尽管还有一些其他方法可以实例化新对象)来实际创建getInstance
返回的对象。
回答by trashgod
In the context of an abstract class, a getInstance()
method may represent the factory method pattern. An example is the abstract Calendar
class, which includes static factory methods and a concrete subclass.
在抽象类的上下文中,getInstance()
方法可以表示工厂方法模式。一个例子是抽象Calendar
类,它包括静态工厂方法和一个具体的子类。
回答by whoismaikl
Yes, this is often used in Singleton Pattern. It`s used when You need only ONE instance of a class. Using getInstance() is preferable, because implementations of this method may check is there active instance of class and return it instead of creating new one. It may save some memory. Like in this example:
是的,这通常用于单例模式。当您只需要一个类的实例时使用它。使用 getInstance() 是可取的,因为此方法的实现可能会检查是否存在类的活动实例并返回它而不是创建新实例。它可能会节省一些内存。就像在这个例子中:
public static DaoMappingManager getInstance() {
DaoProperties daoProperties = null;
try {
daoProperties = (DaoProperties) DaoProperties.getInstance();
} catch (PropertyException e) {
e.printStackTrace();
}
return getInstance(daoProperties);
}
public static DaoMappingManager getInstance(DaoProperties daoProperties) {
if (instance == null) {
instance = new DaoMappingManager(daoProperties);
}
return instance;
}
回答by sodist
Some abstract classes have getInstance() methods because you can't instantiate an abstract class by using new keyword.
一些抽象类具有 getInstance() 方法,因为您无法使用 new 关键字实例化抽象类。
回答by Hassan Sadeghi
For start:
newInstance()
is a static method
. This means you can access to it without creating new class directly! And it creates that class itself "with necessary inits" (via an inner new myClass()
). This means you can add some newInstance
methods (newInstance4usage1, newInstance4usage2,...) for different needs that per static method creates class with different initialization.
Sometimes, this helps class's user (final programmer) to creating class without any worry and very comfortable.
首先:
newInstance()
是一个static method
. 这意味着您无需直接创建新类即可访问它!并且它“使用必要的初始化”(通过内部new myClass()
)创建了该类本身。这意味着您可以newInstance
针对不同的需求添加一些方法(newInstance4usage1、newInstance4usage2...),每个静态方法创建具有不同初始化的类。有时,这可以帮助类的用户(最终程序员)轻松创建类,并且非常舒适。
This helps really when the init process is complex or has important or or usual levels. this method don't prevent from creating class by new
keyword.
当 init 过程很复杂或者具有重要或通常的级别时,这真的很有帮助。此方法不会阻止通过new
关键字创建类。
I like it!
我喜欢!
Excuse me for slow answering, I am typing with my mobile phone!
不好意思,我在用手机打字,回复慢!