This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Best approach for setting the values of Instance Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Best approach for setting the values of Instance Variables" Watch "Best approach for setting the values of Instance Variables" New topic
Author

Best approach for setting the values of Instance Variables

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


Hi ,
Please suggest me as knowing what is the best approach ??

That is to set the values of Instance variables by using an Parameterized constructor as shown below :



Or by explicitly calling the Setter Methods as shown below :




What you suggest , please share your ideas .

Thanks in advnace .


Save India From Corruption - Anna Hazare.
Dieter Quickfend
Ranch Hand

Joined: Aug 06, 2010
Posts: 280
I'd choose the first, given that the second will cause a compiler error.


Oracle Certified Professional Java Programmer
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14669
    
  11

It depends. The constructor can be used as a convenient way to set variables, or not prevent other classes from setting the value again via a setter. A problem arises when you have too many instance variables. Using a constructor to instantiate them all would look ugly and difficult to use. Also, using a constructor with parameters forces other classes to pass values when instantiating the class. This can be inconvenient, unless you have different kind of constructors, like a default constructor.


[My Blog]
All roads lead to JavaRanch
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you .
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Christophe Verré wrote: A problem arises when you have too many instance variables. Using a constructor to instantiate them all would look ugly and difficult to use.

That can solved by implementing a builder.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14669
    
  11

Wouter Oet wrote:
Christophe Verré wrote: A problem arises when you have too many instance variables. Using a constructor to instantiate them all would look ugly and difficult to use.

That can solved by implementing a builder.

For the reference, that's recommended in Effective Java.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(And don't call super() if you're just extending Object -- there's no need for it, and it's misleading.)
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Best approach for setting the values of Instance Variables
 
Similar Threads
methods, instances and arrays - swimming in the ocean of java
Employee with same values is being added two times in HashSet
how comparator works?
Comparable vs Comparator
Overloaded constructors gives Duplicate method in type error