467,878 Members | 1,253 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,878 developers. It's quick & easy.

Using direct access in the constructor?

I'm doing a Java course. Having gone through the use of accessor
(setter and getter) methods for instance variables, it goes on to say:

"For reasons that cannot be elaborated upon here, it is safer, where
possible, to initialise instance variables directly in a constructor
rather than using setter messages."

Can anyone offer a simple explanation for this?

--
Nigel M
Mar 28 '06 #1
  • viewed: 5164
Share:
2 Replies
"Nigel Molesworth" <re***@thegroup.email.invalid> wrote in message
news:kc********************************@4ax.com...
I'm doing a Java course. Having gone through the use of accessor
(setter and getter) methods for instance variables, it goes on to say:

"For reasons that cannot be elaborated upon here, it is safer, where
possible, to initialise instance variables directly in a constructor
rather than using setter messages."

Can anyone offer a simple explanation for this?


I read this book, "Java Puzzlers", which said it was a bad idea to have
your constructors call methods which can be overridden. To prevent methods
from being overridden, you can either mark them as being "final" or as being
"private". They explained why it was a bad idea, giving a code example that
led to a hard to track bug, but I can't remember what it was now. It
certainly made sense at the time, when I read it.

So I'm assuming this is what your book is referring to as well, since
presumably your setter methods are not going to be private, and they may
possibly be non-final.

- Oliver

Mar 28 '06 #2
"Oliver Wong" <ow***@castortech.com> wrote in
news:28eWf.6868$Ph4.4520@edtnps90:
"Nigel Molesworth" <re***@thegroup.email.invalid> wrote in message
news:kc********************************@4ax.com...
I'm doing a Java course. Having gone through the use of accessor
(setter and getter) methods for instance variables, it goes on to say:

"For reasons that cannot be elaborated upon here, it is safer, where
possible, to initialise instance variables directly in a constructor
rather than using setter messages."

Can anyone offer a simple explanation for this?


I read this book, "Java Puzzlers", which said it was a bad idea to
have
your constructors call methods which can be overridden. To prevent
methods from being overridden, you can either mark them as being "final"
or as being "private". They explained why it was a bad idea, giving a
code example that led to a hard to track bug, but I can't remember what
it was now. It certainly made sense at the time, when I read it.

<snip>
The setter method of the derived class has access to the data of the
derived class. However, during contruction of an object of the derived
class, this data has not yet been initialized when the base class object is
constructed and calls the setter method of the derived class.

"Thinking in Java", 3rd ed. Revision 4.0, Chapter 7, makes the point more
clearly than I can (and provides an example that I won't reproduce here:

"Conceptually, the constructor’s job is to bring the object into existence
(which is hardly an ordinary feat). Inside any constructor, the entire
object might be only partially formed—you can know only that the base-class
objects have been initialized, but you cannot know which classes are
inherited from you. A dynamically bound method call, however, reaches
“outward” into the inheritance hierarchy. It calls a method in a derived
class. If you do this inside a constructor, you call a method that might
manipulate members that haven’t been initialized yet—a sure recipe for
disaster."
--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Mar 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
6 posts views Thread by Alexander Stippler | last post: by
25 posts views Thread by rokia | last post: by
11 posts views Thread by Brent Ritchie | last post: by
6 posts views Thread by burningodzilla | last post: by
11 posts views Thread by subramanian100in | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.