• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interfaces and Implementing Classes

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()));
}
reply
    Bookmark Topic Watch Topic
  • New Topic