1

I have a structure similar to the following:

class Foo{
    class Bar{ ... }
    private class Baz{ ... } 
}

Foo and Bar need access to Baz, but Baz needs to remain private to both the module and and other modules importing it.

Questions:

  1. Is there any way to share Baz to Foo and Bar?'
  2. Is there any other class structure that I could use that would allow the wanted access?
Captain Hatteras
  • 490
  • 3
  • 11

1 Answers1

3

You don't need to nest the classes to achieve this.

class Foo {
    // entire module can access
}

class Bar {
    // entire module can access
}

private class Baz {
    // only objects in this file can access
}

If, however, you want to nest the types then you can make use of fileprivate.

class Foo {
    class Bar {}
    fileprivate class Baz {}
} 
trndjc
  • 11,654
  • 3
  • 38
  • 51