Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bug in K& B?

 
Alina Petra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 642:
"From outside the outer class instance code (including static method code within the outer class), the inner class name must now include the outer class's name:
MyOuter.MyInner"

This is not quite true. The inner class name can be MyOuter.MyInner, BUT it can be simply MyInner as well.
 
Alina Petra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to say that in the second case, an import MyOuter.MyInner is required.

Maybe it should be specified in the book that inner classes names can contains simply the name (without the outer class name) as long as the inner classes are imported as: import somePackage.MyOuter.MyInner;
 
Naveen Zed
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont understood your problem.What is happening there?
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alina, what you said is correct. I tested it and it works fine. If we have an outer class MyOuter with inner class MyInner in some package Test then you can refer to Inner class with their simple name 'MyInner' if there is an import statement 'import Test.MyOuter.MyInner'. But this works will work only with public accessiblity. so class MyOuter, MyInner should have public accessibility and also their constructors. But what if I give MyInner class a protected access and try using it in an extended class.

package outer;

public class MyOuter
{
int i;
protected class MyInner
{
final int j = 5;
protected MyInner()
{
System.out.println(j);
}
}
}

package test;

import outer.MyOuter;

class OuterInnerTest extends MyOuter
{
public static void main(String args[])
{
OuterInnerTest t = new OuterInnerTest();
t.test();
}

void test()
{
MyInner in = this.new MyInner();
}
}

This gives me compilation error:
MyInner() has protected access in outer.MyOuter.MyInner

OuterInnerTest class is extending MyOuter class then this should be allowed. right?
 
Alina Petra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The MyInner() constructor is declared protected. Therefore you can not instantiate MyInner class from somewhere else other then the package outer.
 
Rancy Chadha
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package test;
import outer.MyOuter;
class OuterInnerTest extends MyOuter

But I am extending MyOuter class so MyInner() having PROTECTED access should be accessible isn't it. Subclasses in other packages are able to access protected data in superclass . Also I am accessing it using Subclasses reference variable not superclass's reference variable.

Can anyone please clear this doubt.

Thanks,
Rancy
 
Alina Petra
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyInner() class is accessible, you are able to see it, but you can not instantiate it because the constructor has protected access. If the constructor had public access, you could instantiate it as well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic