*
The moose likes Java in General and the fly likes Automatically populate instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Automatically populate instance variables" Watch "Automatically populate instance variables" New topic
Author

Automatically populate instance variables

Martin May
Greenhorn

Joined: Dec 12, 2002
Posts: 5
I would like to automatically populate a class' instance variables with the constructor arguments, e.g. if I have a constructor like this:
SampleClass(String a, int b, Object c)
and my class has the following instance variables:
String a;
int b;
Object c;
I'd like to have my constructor automatically populate these variables (if there is no variable for a constructor argument, just ignore it).
The reason why I'd like to do this is that I'd like to inherit to override the constructor with one that takes more arguments (in a subclass that has additional instance fields), and call super() in the new constructor. However, the old constructor needs all constructor arguments populated to the instance fields because it calls a parameter verification routine.
Since super must be the first statement in the constructor of the subclass, I don't get a chance to populate the instance fields.
Any ideas on how to solve this? I figures that this could be done somehow through reflection.
Thanks,
Martin
Christopher Farnham
Greenhorn

Joined: Sep 18, 2002
Posts: 12
Perhaps I'm oversimplifying but wouldn't it be as simple as:

[ December 12, 2002: Message edited by: Christopher Farnham ]

Christopher Farnham<br />Boston, MA<p>"Perfect is the Enemy of Good"
Martin May
Greenhorn

Joined: Dec 12, 2002
Posts: 5
Not quite
The constructor in class A should call a method verifyParams() which verifies all parameters, also the extended one. Obviously I will override the verifyParams() method in class B, however I can't get to the constructor parameters in they have not yet been populated to the instance variables.
I can't pass them to verifyParams() either because their number and type changes.
Anybody?
Christopher Farnham
Greenhorn

Joined: Sep 18, 2002
Posts: 12
Is it possible that you really want to separate the parameter verification logic from your object construction logic?
When I implement something like this, I usually verifyParams() just before I try to perform an operation, not while I am constructing object itself. After all, the problem you have is that you're trying to verify proper construction before the object is fully constructed.
In my opinion, this would add flexibility while reducing complexity.
Martin May
Greenhorn

Joined: Dec 12, 2002
Posts: 5
Good point, that's actually the way I am implementing things right now. However, I'd still be interested if the aforementioned problem can be solved.
Martin
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Martin- I'm afraid "Hiro Protagonist" is the sort of "obviously fictitious" display name that we discourage here (see our display name policy). Even if it is from a very good book. Please take a moment to change your display name to something more closely resembling a normal human name.
Regarding your question - it's usually considered a bad idea to call an overridable method from a constructor, precisely because it may get called by a base class constructor before the subclass constructor has had a chance to populate all the necessary fields. I'd recommend that you make two private verifyParams() methods instead - one for the base class, and one for the subclass. The base class verifies its own fields, and the subclass does likewise. Since the methods are private the subclass method will not be overriding the base class method - they'll be separate methods, called in succession. Like this:

[ December 12, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you use the constructor for the child class to call the verification routine just after the call super(), there shouldn't be any problems:

I don't think Java will allow what you originally asked. You can't automatically populate members of both the parent and the child. The parent is always populated first, then the child.
HTH
Layne


Java API Documentation
The Java Tutorial
Martin May
Greenhorn

Joined: Dec 12, 2002
Posts: 5
Thank you very much for the insightful replies. I've decided to go with the cascaded parameter verification that was suggested.
My apologies about the name, as you can see I've changed it. That'll teach me for not reading the rules
Best regards,
Martin
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Automatically populate instance variables