• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is this instance an attribute of another instance?

 
Dorcas Rebanha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the application, the class CropStipulation has a description, which is of type String, and it has a transaction which is of type TransactionDeclaration.

In the same application, the class ProgramStipulation has a String description, and it also has a transaction of type TransactionDeclaration.

There is nothing in TransactionDeclaration to indicate when it is an attribute of a CropStipulation or when it is an attribute of a ProgramStipulation.

Is there a method (or something? anything?) that will allow me to determine where a given instance of TransactionDeclaration is from?

Thanks.

Dorcas
 
ronnir paterl
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U can use instanceof to check the class of your object


e.g
if (object instanceof class)
/// your statements here


hope this helps...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dorcas Rebanha:

Is there a method (or something? anything?) that will allow me to determine where a given instance of TransactionDeclaration is from?


No; there's no way within Java to determine from where an object is referenced. Remember that although it may not be possible in your application, it's possible in theory for a single object to be an instance variable in several other objects.
 
Dorcas Rebanha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(sigh)
I was afraid of that. Thanks, anyway.

Dorcas
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's actually one of the cool things about OO (and good modularity in any language.) An object doesn't depend on how it's used, so other folks are free to come up with creative new uses. If you had code that said:

you'd have to modify that code every time you used it in a new place. Back in the previous millennium I worked with COBOL programs that had that dependency and it was way ugly.

You said "I was afraid of that" which hints at some other problem you're trying to solve. What's up? Maybe we can help.
[ October 19, 2007: Message edited by: Stan James ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic