• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces and Implementing Classes  RSS feed

 
Craig Rykal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I want to be able to do:

The Line marked with the *** is the one I have a question about. I realize the way it is written above is wrong. What I would like is the caller of A.get_B_Ifc() to have access to the same Key as that of line +++ where object.getKey() is used. But I would like to do this without creating an instance of B.
Is this possible?
I appreciate any help here... Thanks!
[ January 22, 2003: Message edited by: Cindy Glass ]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are mixing your static syntax and instance syntax so much that I am not sure what you are trying to accomplish.
When you say that you would like to do this "without creating an instance of B" do you mean in the putB(A aInstance) method of Test? SOMEWHERE you have to create the original B that gets put into the HashMap and later pulled out. That is the only place that you are creating a B.
In your
getB(A aInstance) {
*** B_Ifc bInstance = A.get_B_Ifc( B_Ifc.getKey() );
}
You are create a variable of type B_Ifc and then (using static syntax???) invoking an instance method and feeding it (using static syntax???) what is returned from the getKey() method. But you have to get the key of SOMETHING. How will it know what object you are going after?
Perhaps you meant this to read:
getB(A aInstance, B someB) {
*** String bInstance = aInstance.get_B_Ifc( someB.getKey() );
}
Then it would mean that you are looking for the key for this B in the HashMap of this particular A.
Of course since the getKey method actually only returns the name of the class instead of some unique name of this object - all B_Ifc objects will have the same key .
Perhaps you had better explain it some more.
 
Craig Rykal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make the *** line correct, I would have to do the following.
getB(A aInstance)
{
B_Ifc bInstance = new B();
bInstance = A.get_B_Ifc( bInstance.getKey() );
}
The reason I don't want to do the above is because Why create another B? When the B already exists on the HashMap? My new B above will be available for garbage collection as soon as I return the Instance that's in the HashMap.
I realize that using the name of the Class in the HashMap restricts that only one of that Type exists. That is up to the Class that inplements B_Ifc. In my case, I actually want to enforce the singularity.
Does this enlighten you, or just confuse you some more? I know the differences between Instance and Static, but kind of want a way to use the functionality of both. Understand?
Thanks for replying! I appreciate it!
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - so you were just figuring that any instance of B would generate the same key anyway - so the particular instance that you use wouldn't matter. Weird.
Well, when you did:
A.get_B_Ifc( bInstance.getKey() );
you used the Classname.method() syntax which only works if get_B_Ifc is a static method - which it is not in this case (but probably SHOULD be). (not to mention a general disreguard for return types :roll: ).
If the key is just a String that contains the class name any way, why don't you just use that? (Since apparently you don't have to be passed anything to know that you are looking up a B).
B_Ifc getB(A aInstance)
{
return ( aInstance.get_B_Ifc( "B" ));
}
or better yet:
B_Ifc getB(A aInstance, Object x)
{
return ( aInstance.get_B_Ifc( x.getClass().getName()));
}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!