*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
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
Bartender

Joined: Aug 06, 2010
Posts: 504
    
    4

I'd choose the first, given that the second will cause a compiler error.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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: 14687
    
  16

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.)
 
Consider Paul's rocket mass heater.
 
subject: Best approach for setting the values of Instance Variables
 
Similar Threads
Employee with same values is being added two times in HashSet
Overloaded constructors gives Duplicate method in type error
how comparator works?
Comparable vs Comparator
methods, instances and arrays - swimming in the ocean of java