• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bug in K& B?

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont understood your problem.What is happening there?
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic