*
The moose likes Java in General and the fly likes accessing members of an inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "accessing members of an inner class " Watch "accessing members of an inner class " New topic
Author

accessing members of an inner class

venkat paruchuri
Greenhorn

Joined: Aug 15, 2001
Posts: 28
Hi friends,
I got a situation, how to access the memebers of protected inner class from another package.
The exact problem is, in a package1 there are three classes and one interface.
For example, in a package1,
ClassA has protected inner class called ClassB.
ClassB has protected inner class called ClassC.
ClassB implements interface called InterfaceA.
I have a class called ClassD in an other pacakge called package2, which has to access the public members of the protected inner class ClassC in package1.
pictorically,
package1;
class ClassA {
protected class ClassB implements InterfaceA {
protected class ClassC{
}
}
}
package2;
class ClassD{
how to access memebers of ClassC in package1?
}
I appreciate if any body can help me to overcome this problem. Thanks in advance..
[ February 15, 2002: Message edited by: venkat paruchuri ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Well, access issues aside, first you can't access any members of the inner Classes, you can only access members of instances of those inner classes. So you are talking about creating new instances of those inner classes, then accessing the members of those instances.
Now since these inner classes are protected, it means that ONLY classes in the current package, OR subclasses of class 'A' in any package, may access those inner class members.
Since D is neither in the same package, nor is it a subclass of 'A', class 'B' and class 'C' are not accessible to class D.
Basically, you can't access the members of either class from any methods/statements in D.


Rob
SCJP 1.4
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
In other words, the whole idea of "protected" access is to prevent you from doing what you want to here.


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
AFAIK there might be a way to do this using reflection.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I don't think so. The following code throws an IllegalAccessException at the getInt() line:

However it works fine if you change the declaration of value from private to public. There are probably other ways to try this using reflection, but I imagine all reflective methods will eventually encounter the same sort of problems. I'd certainly be interested in hearing about it if there's a way to do this...
[ February 17, 2002: Message edited by: Jim Yingst ]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065

output:
private int Hider.value
value = 17

[ February 17, 2002: Message edited by: Mapraputa Is ]

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Yeah, it is possible.
Here is victimized code:

Here you can get ObjectInspector class that output passed object's state via Reflection.
I get an instance through a public class Secret1 from the same secrets package
Object sc = Secret1.getSecret();
And then called ObjectInspector:
ObjectInspector inspector = new ObjectInspector ();
inspector.output (secret, new FileWriter ("inspect.html"));
output:
secrets.Secret#0
hidden1 int 42
tp secrets.Secret$TopSecret$TopestSecret secrets.Secret$TopSecret$TopestSecret#1
secrets.Secret$TopSecret$TopestSecret#1
this$1 secrets.Secret$TopSecret secrets.Secret$TopSecret#2
hidden int 17
secrets.Secret$TopSecret#2
this$0 secrets.Secret secrets.Secret#0

[ February 17, 2002: Message edited by: Mapraputa Is ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Wow, good catch. I didn't know about setAccess(). Cool, thanks.
venkat paruchuri
Greenhorn

Joined: Aug 15, 2001
Posts: 28
Hi friends,

Thank you very much for your help. I got a clue in my problem. I found a method in InterfaceA which will return an another interface object that is implemented by ClassC. That returned interface object being public object I got access from ClassD in package2. so, my problem is solved.
Once again thanks to all of you for your response.
venkat
[ February 25, 2002: Message edited by: venkat paruchuri ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: accessing members of an inner class
 
Similar Threads
why java does not support Multiple Inheritance?
Parsing a variable
Private variables aren't inherited?
How to find all imported classes in java file?
Subclass calls super class constructor then how many object will be created