473,322 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
2 5428
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
25
by: rokia | last post by:
in a project, I use many,many stl such as stack,list,vctor etc. somewhere the vector's size is more than 2K. is this a efficient way?
17
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset...
1
by: Pradeep Kumar | last post by:
I have a scenario where I am writing a property(using get and set) have to return a member of an object stored in an array of objects! The class user has to pass an index based on which a object i's ...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
5
by: heng | last post by:
class A { public: int x; A(int x_=0):x(x_){} }; int main() { A obj1(99); //user-defined constructor is used
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
11
by: subramanian100in | last post by:
Suppose the following is in Test.h #ifndef TEST_H #define TEST_H #include <iostream> #include <string> using namespace std;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.