aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access to inner class 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access to inner class" Watch "Access to inner class" New topic
Author

Access to inner class

Dmitri Zhuchkov
Greenhorn

Joined: May 16, 2003
Posts: 3
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.
Lawrence Chettiar
Ranch Hand

Joined: Apr 10, 2003
Posts: 62
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


SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
Dmitri Zhuchkov
Greenhorn

Joined: May 16, 2003
Posts: 3
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�
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
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.
peter greaves
Ranch Hand

Joined: Sep 27, 2002
Posts: 51
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.


SJCP 1.2
Rajinder Yadav
Greenhorn

Joined: May 13, 2003
Posts: 27
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??


When faced with an easy thing to do and a hard thing to do, always pick the right thing to do!<p><a href="http://yadav.shorturl.com" target="_blank" rel="nofollow">Rajinder Yadav</a>
Dmitri Zhuchkov
Greenhorn

Joined: May 16, 2003
Posts: 3
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.
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
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?
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access to inner class