11

Suppose I have an outer class with an inner class inside. The inner class has four fields with all possible access modifiers.

class Outer {
    private class Inner {
        public int publicField;
        protected int protectedField;
        int packagePrivatefield;
        private int privateField;
    }

    void doSomethingWithFields() {
        Inner inner = new Inner();
        inner.publicField = 111;
        inner.protectedField = 111;
        inner.packagePrivatefield = 111;
        inner.privateField = 111;
    }
}

The inner class is private, so I can't create instances of it outside the Outer class. Inside the Outer class, if I create an instance of the inner class and try to change the value for each of the fields I will succeed to do that. So I see that there is no sense in access modifiers for the above fields. Is there?

Edited: The main question is: Which access modifiers should I choose for members of the private inner class? Inner class can not only implement interface. I can put some difficult structure with logic into it.

Dumas45
  • 501
  • 1
  • 10
  • 20
  • 4
    I tend to use the same modifiers as if my class was not a private inner class. This at least documents that this method shouldn't be called from the outside whereas this method is intended to be called from the outside. It also helps when refactoring the class to a top-level class. – JB Nizet Aug 08 '14 at 13:57
  • 1
    I'm sorry, but the duplicate by assylias is invalid: this question is specifically about *fields*, whereas the question marked as duplicate deals with *methods*, and the reasoning is completely different for them. – Marko Topolnik Aug 08 '14 at 14:19

4 Answers4

7

Access modifiers have a say in inheritance. Another inner private class that extends that class you talk of does not have the privileges of the outer class.

public class Main {
    private class Test {
        protected int hello;
    }
    private class TestNext extends Test {
        private TestNext() {
            this.hello = 1;
        }
    }
}

Will compile, but if hello was private, it would not.

Xabster
  • 3,710
  • 15
  • 21
  • 1
    I would add here that having modifiers (and using getters and setters instead of the fields) is safer in case you ever refactor the code to pull out the class. – John B Aug 08 '14 at 13:59
  • 1
    @JohnB definitely, but given the question I didn't see any reason to mention that. I didn't comment on his class names, or any such. Just giving an example where access modifiers inside private inner classes matter. – Xabster Aug 08 '14 at 14:05
0

Right, if the inner class is private it doesn't make sense.

But keep in mind that inner classes are not very beautiful in general, you should rather group multiple classes with packages.

maxdev
  • 2,491
  • 1
  • 25
  • 50
0

Yes. You are correct. But only if you have single nested inner There is no different between having various access modifiers in a private inner class. Nothing can be seen out of the class. But if you have another nested inner class then your access modifiers will matter. e.g.

class A{
   private class B{
      class C{}
   }
}

in this case class B's field and methods access modifiers will matter

0

If the inner class does not implement any Interface or inheritance, then there is no point of specifying access modifiers.

If the inner class inherits something, it does make sense in that context.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Vignesh Paramasivam
  • 2,360
  • 5
  • 26
  • 57