my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bug in K& B? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bug in K& B?" Watch "Bug in K& B?" New topic
Author

Bug in K& B?

Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
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

Joined: Oct 17, 2006
Posts: 26
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

Joined: Aug 11, 2006
Posts: 61
I dont understood your problem.What is happening there?
Rancy Chadha
Ranch Hand

Joined: Jul 12, 2006
Posts: 135
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?


Thanks,<br />-Rancy
Alina Petra
Greenhorn

Joined: Oct 17, 2006
Posts: 26
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

Joined: Jul 12, 2006
Posts: 135
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

Joined: Oct 17, 2006
Posts: 26
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bug in K& B?