3

I have a class with private fieds and also a static abstract inner class with the generic type extending the outer class type, that tries to access the outer class private fields but get's the following error: - error: a has private access in Outer - error: doSomething has private access in Outer

See code below:

public abstract class Outer extends SomeOuter
{
    private Object a;
    private void doSomething(){}

    public static absract class Inner<T extends Outer> extends SomeOuter.SomeInner<T> {     
        public InnerMethod(T p) {
            p.a; //error: a has private access in Outer
            p.doSomething() //error: doSomething has private  access in Outer
        }
    }
} 

I'm compiling using jdk 1.7

Can anyone please tell me why am getting this error.

6 Answers6

3

An Inner class can access private field of enclosing class and a static inner class can also access any private members of enclosing class .

The class itself isn't really "static"; there's no such thing as a static class. The static modifier in this case says that the nested class is a static member of the outer class. That means it can be accessed, as with other static members, without having an instance of the outer class.

Since it is a static member , it is able to access outer class's private members , because within class private members are accessible.

eg.

class One
{
    private int i=0;
     class Two
    {
         void go()
            {
                System.out.println(new One().i);  //accessible
            }
    }
}
class two 
{
    private  int i=3;
    static class one
    {
        void go()
        {
            System.out.println(new two().i);    //accessible in static class
        }
    }
}

But here ,

Inner<T extends Outer> extends SomeOuter.SomeInner<T>

T is a class which extends Outer , doesn't mean it is inner. That why it is giving error.

anshulkatta
  • 2,044
  • 22
  • 30
2

thats how private modifier works if you ever declare any method or variable as private than that things can not be accessed out side the class

shreyansh jogi
  • 2,082
  • 12
  • 20
2

A static embedded class is effectively an outer class. It can't access the private members of another class. See the accepted answer to:

Static nested class in Java, why?

Community
  • 1
  • 1
Buurman
  • 1,914
  • 17
  • 26
1

Both the Object and the function you're trying to use are declared as private, which means they cannot be used outside the Object. If you want to use them in child classes as well, declare them as protected.

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
1

Changes the scope of the fields on the Outer class to protected so that classes extending Outer may access these fields.

public abstract class Outer extends SomeOuter
{
    protected Object a;
    protected void doSomething(){}


    public static absract class Inner<T extends Outer> extends SomeOuter.SomeInner<T> {     
        public InnerMethod(T p) {
            p.a; //error: a has private access in Outer
            p.doSomething() //error: doSomething has private  access in Outer
        }
    }
} 
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

Thats what you have private modifier for. Though it is the inner class, it cannot access the private members of the outer class. So, declare it as protected, as you are extending the outer class to inner class

Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69