Difference between calling new and getInstance()

前端 未结 6 1826
一个人的身影
一个人的身影 2021-01-31 11:30

Is calling Class.getInstance() equivalent to new Class()? I know the constructor is called for the latter, but what about getInstance()? T

相关标签:
6条回答
  • 2021-01-31 12:20

    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 default constructor. Here's an extract of its Javadoc:

    Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized.

    In code,

    Object instance = Object.class.newInstance();
    

    is the same as

    Object instance = new Object();
    

    The Class#newInstance() call actually follows the Factory Method pattern.


    Update: seeing the other answers, I realize that there's some ambiguity in your question. Well, places where a method actually named getInstance() is been used often denotes an Abstract Factory pattern. 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.

    Further you also see this methodname often in some (mostly homegrown) implementations of the Singleton pattern.

    See also:

    • Real world examples of GoF design patterns.
    0 讨论(0)
  • 2021-01-31 12:21

    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.

    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.

    I like it!


    Excuse me for slow answering, I am typing with my mobile phone!

    0 讨论(0)
  • 2021-01-31 12:27

    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.

    0 讨论(0)
  • 2021-01-31 12:30

    Some abstract classes have getInstance() methods because you can't instantiate an abstract class by using new keyword.

    0 讨论(0)
  • 2021-01-31 12:31

    Absolutely (usually) not.

    getInstance is the static method often used with the Singleton Pattern in Java. The new keyword actually 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.

    0 讨论(0)
  • 2021-01-31 12:35

    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:

        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;
    }
    
    0 讨论(0)
提交回复
热议问题