Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private inner class access

 
Viktor Kubinec
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given this code:


Why //1 compiles? Is it ok that this statement returns object of Type which is not acessible from this place?
Why //3 compiles (and prints "I AM HERE" , which means that toString method was accessed)
I understand why //2 doesn't compile, I just put it here to show that toString method cannot be called here.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Viktor Kubinec wrote:Given this code:


Why //1 compiles? Is it ok that this statement returns object of Type which is not acessible from this place?

Yes, it's fine. All that matters is that A, newA(), and A.lala() are accessible. It's very common to do this. For instance, I think some Collections' iterator() methods return instance of private Iterator implementations.

Why //3 compiles (and prints "I AM HERE" , which means that toString method was accessed)


Because println(Object) calls some other methods, which eventually end up calling Object.toString(), which is accessible.

I understand why //2 doesn't compile, I just put it here to show that toString method cannot be called here.


The difference is that //3 is println(Object), but //2 gives a reference to the inaccessible B type. If you change that to


then it will compile.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic