Reputation: 11163
I have found some blog where there is a suggestion of avoiding new
keyword while
creating object of a class. Some examples of creating object without the new
keyword are-
SampleObject obj = Class.forName("com.example.SampleObject").newInstance();
Or using clone()
method -
SampleObject obj1 = new SampleObject();
SampleObject obj2 = obj.clone();
Then here I have found some good examples of creating object without new
keyword
I can understand the advantages of 'Factory' pattern while avoiding new
keyword from main portion of code. If I am not using any design pattern (for creating objects) is there any benefit of creating object without the new
keyword? Or what are the reasons for creating of object without new
?
Upvotes: 5
Views: 7178
Reputation: 10717
When you use Class.forName, the class name can be set at runtime. That can be useful for drivers e.g.
When you use new, instead, the class name of the instance you're creating is hard coded. In general, this creates high coupling, and should be avoided (Except when you're absolutely sure you'll always need this coupling).
In short, if you want to create an instance of a specific class, new
makes no harm.
However, you should ask yourself:
new
by a singleton or a pool).new
by Dependency Injection or the Factory Pattern).Upvotes: 1
Reputation: 1911
there is no replacement for new
, even Class.forName("XYZ").newInstance()
HAS to work exactly like new
, the only difference being that you're hardcoding the full class (or even package...class
) hence actually achieve the opposite of what you're trying to do, with new
the instance may be dynamically resolved and/or even injected.
If you inject an instance of interface A
you may very well get a bean of class B
which implements A
- thats in fact more dynamic and complements factory patterns very well. Thats hardly possible if your classname is hardcoded, at least in my humble opinion.
Upvotes: 1
Reputation: 200148
What you read in the blog must have been about factory methods and similar techniques, which don't avoid new
, but rather place it behind a more flexible API.
There are definetely no overarching downsides to the new
operator. It has been, and will remain, the staple of any Java code, and the most natural way to actually create an object, as opposed to satisfying a more generic concern, such as "provide me with an entry point to your API".
All other techniques that create objects without involving new
are special-purpose tools (cloning, deserialization, etc.).
Upvotes: 9
Reputation: 11433
Complex Objects
In general you may consider avoiding new
for reasonably complex objects with non-trivial creation. You can consider some factory/builder which add versatility.
For example if you create a big composite object it is good to use builder pattern. (Long example here)
Simple Objects
For simple objects (most common case) it is best to stick with new
.
For example if you have simple class
public class Dog{
private string name;
//getter + setter
}
Then it is an overkill to create factory for this and you should call it by new Dog()
Dependency Injection
In enterprise applications you commonly use dependency injection, which let's you avoid using new
explicitly. You won't remove all object instantiations, but it provides nice improvement.
(Basic Spring tutorial here)
Upvotes: 2
Reputation: 40500
If you like sluggish, slow code, that is hard to debug, maintain and understand, absolutely, avoid using new
keyword at all costs, obfuscate as much as possible!
Upvotes: -4