Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scope of private constructor in Nested Class

This is more of a puzzle than question. I have the following code:

public class PrivateBaseConstructor {     public static class BaseClass {         private BaseClass() {         }      }      public static class DerivedClass extends BaseClass {         public DerivedClass() {             super(); // 1*         }     } } 

Here the call for super(); at 1* is allowed even though the base class constructor is private. If we write the classes as separate classes in same package:

BClass.java public class BClass {     private BClass() {      } }  DClass.java public class DClass extends BClass {     public DClass() {         super(); // 2*     } 

The compiler rightly gives an error at 2* since the base class constructor is not visible.

Why doesn't the compiler throw an error in my first scenario when both the classes are declared static within one class?

like image 854
Anshu Avatar asked Sep 22 '12 08:09

Anshu


People also ask

What is the scope of be used for nested class?

The scope of a nested class is bounded by the scope of its enclosing class. Thus in below example, class NestedClass does not exist independently of class OuterClass. A nested class has access to the members, including private members, of the class in which it is nested.

Can nested classes access private members?

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.

Is constructor should be declared in the private scope?

Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.

Can private constructor be accessed?

Private constructors in Java are accessed only from within the class. You cannot access a private constructor from any other class. If the object is yet not initialised, then you can write a public function to call the private instructor.


2 Answers

if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.1

like image 157
irreputable Avatar answered Sep 16 '22 22:09

irreputable


Because nested classes can see each others members. This has nothing to do with the static declarations. See the following example of your code with just nested inner classes (not static).

public class PrivateBaseConstructor {     public class BaseClass {         private BaseClass() {}     }      public class DerivedClass extends BaseClass {         public DerivedClass() {             super(); // 1*         }     }      public static void main(String[] args)     {        new PrivateBaseConstructor(). new DerivedClass();     } } 

Read more about nested classes here: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

like image 42
KappaMax Avatar answered Sep 20 '22 22:09

KappaMax