Benefits of preventing "new" Keword when creating an object in Java
I found a blog where there is a suggestion to avoid the keyword while creating the class object. Some examples of creating an object without a keyword are - new
new
SampleObject obj = Class.forName("com.example.SampleObject").newInstance();
Or using the method clone()
-
SampleObject obj1 = new SampleObject();
SampleObject obj2 = obj.clone();
Then here I found some good examples of creating an object without a keyword new
I can understand the benefits of the Factory pattern while avoiding the keyword from the main body of code. If I am not using any design pattern (for creating objects), is there any advantage of creating an object without a keyword ? Or what is the reason for creating an object without ? new
new
new
source to share
What you read on the blog should have been about factory methods and similar methods, which doesn't avoid new
, but rather puts it in a more flexible API.
The operator has new
no clear flaws. It has been and will remain a staple of any Java code and is the most natural way to actually create an object, rather than address a more general problem like "give me an entry point to your API."
All other methods that create objects unattended new
are special-purpose tools (cloning, deserialization, etc.).
source to share
Complex objects
In general, you can opt out new
for rather complex objects with non-trivial creation. You may want to consider some factory / builder that add versatility.
For example, if you are creating a large composite object, it is helpful to use the builder pattern. ( Long example here )
Simple objects
For simple objects (most common case), it's best to stick with new
.
For example, if you have a simple class
public class Dog{
private string name;
//getter + setter
}
Then it would be overkill to create a factory for this and you have to call it new Dog()
Dependency injection
In enterprise applications, you usually use dependency injection which allows you not to explicitly use it new
. You will not delete all object instances, but it does provide a nice improvement. ( Basic Spring tutorial here )
source to share
When you use Class.forName, the class name can be set at runtime. This can be useful for drivers, for example.
When using new instead, the class name of the instantiated instance is hardcoded. In general, this creates high coupling and should be avoided (unless you are absolutely certain that you will always need this coupling).
In short, if you want to create an instance of a particular class , new
is not harmful.
However, you must ask yourself:
- If you really need a new instance (otherwise you should replace
new
with singleton or pool). - If you will always use the same class for this instance (otherwise you should replace
new
with Dependency Injection or Sample Factory ).
source to share
there is no substitute for new
, even Class.forName("XYZ").newInstance()
HAS works exactly the same as new
, with the only difference that you are hard-coding the full class (or even package...class
), so you actually achieve the opposite of what you are trying to do, with the new
instance can be dynamically resolved and / or even introduced.
If you inject an instance interface A
, you can very well get a bean of the class B
that implements A
- it's actually more dynamic and complements the factory patterns very well. This is hardly possible if your class name is hardcoded, at least in my humble opinion.
source to share