E
E
EVGENY T.2018-10-18 11:34:12
Java
EVGENY T., 2018-10-18 11:34:12

Why newInstance instead of a constructor?

In books I met such an approach, instead of a constructor, a static method is used that returns an instance of the class.

class Class1 {

    public static Class1 newInstance() {
        return new Class1();
    }

}

I suspect this is some kind of design pattern? Why do this? What does such an approach give?
Recommend an intelligent book on this issue (but not with a crazy woman on the cover).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Konyukhov, 2018-10-18
@Beshere

In your example, you have a defective singleton
Pattern Book - Gang of Four

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question