Forums Register Login

Access to inner class

+Pie Number of slices to send: Send
1 public class Outer{
2 private Inner inner = new Inner();
3
4 public Inner getInner(){
5 return inner;
6 }
7
8 private class Inner{
9 private int i = 1;
10 int getI(){
11 return i;
12 }
13 }
14 }
15
16 public class Test{
17 public static void main(){
18 Outer outer = new Outer();
19 System.out.println( outer.getInner().getI() );
20 }
21 }
Why this code compile successfully? Inner class has private visibility so Test class shouldn�t have access to it methods nerveless compilation succeeded and compiler doesn�t failed on line 19 where Test class accessing to private Inner class.
+Pie Number of slices to send: Send
Well this was discussed earlier here may be if you search a bit.
But in summary if you see that the code is complaint to OOP rules and implementing the data hiding logic where a private member is exposed via a public method and a private inner class is also a member hence it fits the above rule.
Somebody may correct me if my thinking is wrong
+Pie Number of slices to send: Send
Thank you for so quick reply. But not only instance of Inner class is private also class by itself declared as private.
More over when I changed getI() visibility to public then compilation failed with :
class Outer.Inner is not public, therefore method getI() cannot be accessed from outside package
Any suggestions�
+Pie Number of slices to send: Send
This is tricky and difficult to tell beforehand.
The code compiles because Inner class is not mentioned in the access expression.
It seems that the compiler complains if it finds an invocation of a public method declared in a private class.
+Pie Number of slices to send: Send
This file should not compile, there are two public classes in the same file!
Secondly when using an IBM JDK (and If I do remove one of the public modifiers) I do get a compilation error
"The method getI() from the type com.ibm.tools.Outer.Inner is not visible"
I know that the exam is going to be on the Sun JDK, but what JDK is correct?
I did see the other posting in regards to this topic and I think the last update stated that someone was checking with Sun on this matter.
It appears that the IBM JDK is working correctly.
Even though getInner() return an instance of the Inner class, Inner is still private and its members should not be visible outside the class.
Someone please correct me if I'm wrong.
+Pie Number of slices to send: Send
but doesn't getInner() return the instance but within the scope of an instance of outer? so as long as outer can "see" it, then it is ok? all the references in Test are to publically available items : an instance of Outer, and a public method of outer. hmm. it makes sense to me but hey lots of things do when they probably shouldn't.
+Pie Number of slices to send: Send
If you examine the code class Test has access to a reference of class Outer which is ok. Also the method 'int getI()' in not declared private inside the Inner class, it has default access privilege. So the code should compile and run ok.
If you add private to the:
int getI()
then you should see a compile time error like so:
test.java:25: getI() has private access in Outer.Inner
System.out.println( outer.getInner().getI() );
^
1 error

My guess is that declaring Class Inner as private has to do with visibility when subclassing is invloved? Can someone clear this up for me??
+Pie Number of slices to send: Send
I used SUN JDK 1.3.1
If you add public (!PUBLIC!) to the:
int getI()
then you should see a compile time error like so:
Test.java:16: getI() in Outer.Inner is not defined in a public class or interface; cannot be accessed from outside package
System.out.println( outer.getInner().getI() );
^
1 error
When 'int getI()' has default (!DEFAULT!) visibility compilation succeeded.
I do not see any sense in this compiler error message. If compiler allows access to method declared with default visibility, so why it deny access to public method.
+Pie Number of slices to send: Send
I think this is a bug in the Sun JDK.
My thoughts (and please someone correct me if I'm wrong) is that Inner class is private and the above code should not compile.
If for example you have the following code

The get_num() method returns the value of num.
However when you get an instance of the Inner class you are getting a reference and not a value.
Inner is still a private class and the reference still points to a private class, so you should not be able to see any of the Inner members.
I could not find any documentation to explicity prove or diprove my theory.
Could someone please clear this up for everyone!
I guess the most important question of all is how should we answer this on the test?
+Pie Number of slices to send: Send
I think this is a bug in the Sun JDK.
My thoughts (and please someone correct me if I'm wrong) is that Inner class is private and the above code should not compile.
If for example you have the following code

The get_num() method returns the value of num.
However when you get an instance of the Inner class you are getting a reference and not a value.
Inner is still a private class and the reference still points to a private class, so you should not be able to see any of the Inner members.
I could not find any documentation to explicity prove or diprove my theory.
Could someone please clear this up for everyone!
I guess the most important question of all is how should we answer this on the test?
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 893 times.
Similar Threads
how to access Inner class member
Access to Inner Class private members
this(i)
a method in a private class is accessible
Private variable access
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:40:03.