Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "new A()" and "A.newInstance()"?

When should I prefer one over the other? What is the purpose of the method shown below?

class A {
    public static A newInstance() {
        A a = new A();
        return a ;
    }
}

Can someone explain to me the differences between these two calls?

like image 504
rex Avatar asked Jan 17 '12 03:01

rex


People also ask

What is difference between new operator and newInstance () method?

In Java, new is an operator where newInstance() is a method where both are used for object creation. If we know the type of object to be created then we can use a new operator but if we do not know the type of object to be created in beginning and is passed at runtime, in that case, the newInstance() method is used.

What does Class newInstance () do?

Class. newInstance() throws any exception thrown by the constructor, regardless of whether it is checked or unchecked. Constructor. newInstance() always wraps the thrown exception with an InvocationTargetException .

What is the difference between getInstance and new?

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.

What is newInstance in Java?

newInstance() 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. .


2 Answers

newInstance() is often used as a way to instantiate an object without directly calling the object's default constructor. For example, it is often used to implement a Singleton design pattern:

public class Singleton {
    private static final Singleton instance = null;

    // make the class private to prevent direct instantiation.
    // this forces clients to call newInstance(), which will
    // ensure the class' Singleton property.
    private Singleton() { } 

    public static Singleton newInstance() {
        // if instance is null, then instantiate the object by calling
        // the default constructor (this is ok since we are calling it from 
        // within the class)
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

In this case, the programmer forces the client to call newInstance() to retrieve an instance of the class. This is important because simply providing a default constructor would allow the client access to multiple instances of the class (which goes against the Singleton property).

In the case of Fragments, providing a static factory method newInstance() is good practice since we often want to add initialization arguments to the newly instantiated object. Rather than having the client call the default constructor and manually set the fragment arguments themselves, we can provide a newInstance() method that does this for them. For example,

public static MyFragment newInstance(int index) {
    MyFragment f = new MyFragment();
    Bundle args = new Bundle();
    args.putInt("index", index);
    f.setArguments(args);
    return f;
}

Overall, while the difference between the two is mostly just a matter of design, this difference is really important because it provides another level of abstraction and makes code a lot easier to understand.

like image 167
Alex Lockwood Avatar answered Sep 30 '22 04:09

Alex Lockwood


In your examples they are equivalent and there is no real reason to chose one over the other. However, using newInstance is commonly used if to perform some initialization prior to handing back an instance of the class. If every time you request a new instance of the class by calling its constructor, you end up setting a bunch of instance variables before you can use the object, then it would make more sense to have the newInstance method perform that initialization and return to you an object that's ready to use.

For example, Activitys and Fragments aren't initialized in their constructors. Instead, they are generally initialized during onCreate. Therefore, it's common practice for the newInstance method to accept whatever parameters the object will need to use during initialization, and it stores them in a Bundle that the object can read from later. An example of this can be seen here:

Sample class with newInstance method

like image 45
David C. Sainte-Claire Avatar answered Sep 30 '22 03:09

David C. Sainte-Claire