Is the constructor of a private inner class also private?

I am refactoring an android project that is getting big. Running lint gives me the JSME question Private member access between outer and inner classes. Given the following example

public class Outer { private Inner mInner = new Inner(); private class Inner {} } 

I get information

 Name private field Inner mInner Location class Outer (default package) Problem synopsis Access to private member of class 'Inner' at line 2 Problem resolution Make 'Inner' constructor package-local 

Applying a solution to a problem changes the source to

 public class Outer { private Inner mInner = new Inner(); private class Inner { Inner() {} } } 

I'm a little confused at the moment. Until now, I thought the example would be equivalent

 public class Outer { private Inner mInner = new Inner(); private class Inner { public Inner() {} } } 

Am I mistaken in this case or is it a lint problem?

+7
java android android-studio inner-classes android-lint
source share
2 answers

Section 8.8.9 of the Java Default Language Constructor Specification says:

In a class, if the class is declared public, by default the constructor implicitly gets the public access modifier (ยง6.6); if the class is declared protected, then the default constructor implicitly takes into account the protected access modifier (ยง6.6); if the class is declared private, then the default constructor implicitly sets the private access modifier (ยง6.6); otherwise, the default constructor has default access implied by the access modifier.

+2
source share

You are mistaken in your understanding, but linter is not particularly clear, and the advice is probably not related to Android (this is not J2ME).

As David explained, the internal implicit default constructor for the inner class has the same access modifier as the class itself, but private members are available in the same compilation unit (Java file). There is no language reason to avoid a private constructor.

However, internally, since classes are compiled into separate output files, the compiler must create synthetic adapter methods to give classes access to private members. The lack of runtime of these methods does not matter for desktop applications, but for something the same as J2ME, the difference can be justified if access to the element will be available directly (using the package area).

Android does significant post-processing of class files, and Android devices are not as limited as J2ME devices. If you do not type code for both platforms, I would change the lint configuration.

+1
source share

All Articles