user2161473
user2161473

Reputation: 39

Does a constructor of private class has to be private?

If a class is private then must the constructor be private as well?

Upvotes: 3

Views: 621

Answers (5)

sulai
sulai

Reputation: 5354

It does not have to be private. But it can. Example:

public class Outer {

    // inner class with private constructor
    private class Inner {
        private Inner() {
            super();
        }
    }

    // this works even though the constructor is private.
    // We are in the scope of an instance of Outer
    Inner i = new Inner();

    // let's try from a static method
    // we are not in the scope of an instance of Outer
    public static void main(String[] args) {

        // this will NOT work, "need to have Inner instance"
        Inner inner1 = new Inner();

        // this WILL work
        Inner inner2 = new Outer().new Inner();
    }
}

// scope of another class
class Other {
    // this will NOT work, "Inner not known"
    Inner inner = new Outer().new Inner(); 
}

It doesn't make a difference if you use private or public constructor on private inner classes. The reason is that the inner class instance is part of the outer class instance. This picture says it all:

Inner class is part of the outer class. That's why all private members of the inner class can be accessed from the outer class.

Note that we are talking about an inner class. If the nested class was static, the official terminology is static nested class, which is different from an inner class. A public static nested class would be accessible without outer class instance just by calling new Outer.Inner(). See here for more information about inner- and nested classes. http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Upvotes: 0

Karthik T
Karthik T

Reputation: 31952

If you mean nested class, the answer is no. Making the inner class private makes it only usable within the outer class.

Edit: It appears that outer classes have full access to the innards of the inner classes regardless of their access modifiers. This invalidates my above reasoning, but regardless, there is no such restriction. Curiously though, now it appears that if the inner class is private, its constructor is essentially private, regardless of its access modifier, since noone else can call it.

Upvotes: 3

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

No it hasn't. On the contrary, if you create an instance of the inner class using a private constructor (which is default for a private class) from the outer class Java will create an additional class to prevent access violation and keep JVM happy

If you compile this class

class Test {
    private class Test2 {
        Test2() {
        }
    }
    Test() {
        new Test2();
    }
}

javac will create Test.class, [email protected]

and if you compile this class

class Test {
    private class Test2 {
    }
    Test() {
        new Test2();
    }
}

javac will create Test.class, [email protected], Test$1.class

Upvotes: 1

NPE
NPE

Reputation: 500227

No, there is no such restriction. See JLS §8.8.3. Constructor Modifiers.

It's worth pointing out that only a nested class can be declared private. The JLS permits the constructors for such a class to use any valid access modifiers.

Upvotes: 4

Patriks
Patriks

Reputation: 1012

No it is not fix, you can set it private/public/any you want.

But in some case I prefer to make constructor private, when you don't want to allow other classes to create object of this class. then in that case you can do something like this, by setting constructor private.

private class TestClass{
    private TestClass testClass=null;
    private TestClass(){
         //can not accessed from out side
         // so out side classes can not create object
         // of this class
    }

    public TestClass getInstance(){
      //do some code here to
      // or if you want to allow only one instance of this class to be created and used
      // then you can do this
      if(testClass==null)
           testClass = new TestClass();

      return testClass;
    }
}

Btw it depends on your requirement.

Upvotes: 0

Related Questions