aspose file tools*
The moose likes Beginning Java and the fly likes externally accessing private Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "externally accessing private Inner class" Watch "externally accessing private Inner class" New topic
Author

externally accessing private Inner class

Sam Smoot
Ranch Hand

Joined: Apr 18, 2002
Posts: 238
Is there a way to access an inner class that has been declared private externally? I have the following situation:

Class A:

which has the following inner class:

and I have a Calling class B that has a method to collect all the data from Class A, including the information from the inner class... I can get the initial information from Class A, but I can't access the inner class..
Suggestions?


CNSS/NSA Infosec Professional,<br />Software Engineer
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hey Sam,
I'm a little confused on what you are doing. Are you using reflection to get the methods and vars from class A? The name that would be assigned for the inner class is A$ActivityFeeRanges if that helps.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Sam Smoot
Ranch Hand

Joined: Apr 18, 2002
Posts: 238
Actually, I'm not sure how I'm supposed to do it. I was given the class and asked to write an iplementation class to call this and get all the information from a database. The implementation class (Class B) invokes getters / setters from the outer class A and then, when I try to access the g/s's for the inner class (private in A), I can't see them... I can't seem to find a good example (most have only the outer class (A) talking to the inner class (A) , not external classes (B)) and I'm stuck.
[ August 05, 2002: Message edited by: Sam Smoot ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Sam,
Well buddy, you do have a problem. You'll never be able to call any methods on a private inner class except from the enclosing class which must instatiate an object of that inner class. Even the containing class may not call private methods on the private inner class, but the private inner class can call private methods on the containing class. So you are faced with two possibilities to accomplish your task:
1. Change the private inner class to a package inner class (just remove the private modifier).
2. Create public (or package) methods on the enclosing class to get at the private inner class.
Of the two solutions, number two is probably the better, since you don't have to "crack open" a class and violate encapsulation. After all, the designer of the class had a reason for making it private in the first place.
Hope this helps,
Michael Morris
Sam Smoot
Ranch Hand

Joined: Apr 18, 2002
Posts: 238
Can you show an example of each (or know where there are some SIMPLE examples to link to)?
Thanks.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Sam,
After looking at your code again, option 2 is definitely the way to go because your inner class extends ArrayList which is a public class. Now the question becomes: How may instances of the inner class does the enclosing class have? If it's just one then all you have to do is create this getter in the enclosing class:

Now that assumes that the single instance is named
activityFeeRanges. Then you simply call this method from class B and you have an ArrayList that you can pull the data out of. If there is more than one instance then you could change the signature thus:

assuming that there is an array of such objects.
Hope this helps,
Michael Morris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: externally accessing private Inner class