aspose file tools*
The moose likes Java in General and the fly likes Inner class stumbling block ...? 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 "Inner class stumbling block ...?" Watch "Inner class stumbling block ...?" New topic
Author

Inner class stumbling block ...?

D Lor
Greenhorn

Joined: Sep 20, 2006
Posts: 1
Hi,
Perhaps I'm going about this entirely the wrong way (in which case please inform me of this!); but assuming I'm on the right track, I can't for the life of me figure out how to properly encapsulate the details of my inner class through delegate methods in the outer class.

Here's the example code:

package some.package;

Public Class OuterClass {

private ArrayList<InnerClass> myList;
.
//constructor + getters,setters for various things
.
public addInnerClassItem(Map someMap) {
myList.put
}

private class InnerClass {
private Map<String, String> hashMap;
private InnerClass(Map <String, String> someMap) {
hashMap = new HashMap<String, String>(someMap);
.
//getters + setters + some other variables
.
}
}
}

Now, in some other file:

import some.package.OuterClass;

Main() {

OuterClass oC = new OuterClass();
Map<String, String> someMap = new HashMap<String,String>;
someMap.put("exampleKey", "exampleValue");
oC.addInnerClassItem(someMap);
iterator it = oC.getMyListIterator(); //retuns iterator to myList
while (it.hasNext() {
//how do I get access to the HashMap for the .next() instance of InnerClass?
//I want to get the key-value-pairs...
}
}

So as you can see (I hope...), I can't assing anything to the it.next() because within my scope, there is no notion of InnerClass - which is what it.next() returns. Is there a way to sort of "ignore" the fact that my scope doesn't know about InnerClass, but yet use it so that I can do something like:

HashMap = it.next().getHashMap() ??

Any thoughts are massively appreciated. I'm thinign the answer to this is that I don't want to use inner classes in this way. Which is fine...if you can point me in the direction of a better way to do this? I want to have some class that contains instances of another class. The end user (this is a library) needs to be shielded as much as possible from the inner class.

Cheers,
D.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
It's a bit difficult to see exactly what you're up to, because the pasted code is not in CODE tags, is badly formatted and clearly has important bits missing. Can you edit your original post to improve it?

I have a guess at what you might be needing, anyway. You can declare a public interface and make your inner class implement it. The interface would allow access to the HashMap. The outer class can return an iterator typed to the interface. Thus, the outer class never needs to know about the private inner class implementing the interface.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inner class stumbling block ...?