Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

externally accessing private Inner class

 
Sam Smoot
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sam Smoot
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show an example of each (or know where there are some SIMPLE examples to link to)?
Thanks.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic