Solved

Should one define a variable with new() in a class or should one inject it

Posted on 2014-12-18
4
92 Views
Last Modified: 2015-02-10
Hi,
I have a class :
public class AuthenticationManager {

    private static final Logger _logger = LoggerFactory.getTrimmer(AuthenticationManager.class.getSimpleName());
    private final List<Listener> _listeners = new ArrayList<Listener>();
    private boolean _verificationInProgress = false;
    private final CallStateReceiver callStateReceiver = new CallStateReceiver();


I was wondering is it a good practice to have the line :
    private final CallStateReceiver callStateReceiver = new CallStateReceiver();

or should one chnage it to private final CallStateReceiver callStateReceiver ; and get it set from outside ?

What way can we define this ?

Thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40506851
If you instantiate the object directly within the class, then you introduce a dependency on the "new" object within the class. This means your class is tightly coupled with your "new" object. A more flexible design, as you've mentioned, would be to inject the instantiated object into the class, either via the constructor or a method. Even better, you might define an interface that lays out the minimum criteria that your class expects from an object. Then you work with the interface type internally. You don't have to know what kind of object you actually have as you know what to expect from your object. In this way, different callers can have different object types, but they can each still work with your class because they each meet the minimum criteria set forth by your interface definition.
0
 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 40506901
Agree with kaufmed.

If you instantiate the object inside the class, your code is tightly coupled and dependent on the objects created using new(). Best approach is, as suggested by kaufmed, to provide the objects from outside.

Consider a case in future if you have another flavor of CallStateReceiver (say MyCallStateReceiver) and you want to use it in place of CallStateReceiver. How will you do it ? It is not possible if you instantiate the object inside the class, you will need to have mechanism (ie. setter or constructor) where objects can be provided outside the class.

Hope the explains.
0
 

Author Comment

by:Rohit Bajaj
ID: 40508860
HI,
I did not understand the point -
Consider a case in future if you have another flavor of CallStateReceiver (say MyCallStateReceiver) and you want to use it in place of CallStateReceiver. How will you do it ? It is not possible

MyCallStateReceiver will also have the object inside it.. how is it not possible. please clarify exactly what you mean.

thanks
0
 

Author Comment

by:Rohit Bajaj
ID: 40508879
Hi,
Can you please give an example of :
 Even better, you might define an interface that lays out the minimum criteria that your class expects from an object.
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question