-2

What is default type of constructor e.g. public/private/astract etc.

Can we change the type of constructor in java?

When I creating constructor as private its not allowing to access the class why?

Altmish-E-Azam
  • 1,561
  • 1
  • 13
  • 24

5 Answers5

1

You can define your constructor in the following ways:

  • public A() — That means that every other class can create an A instance.
  • protected A() — Protected access means it can only be accessed by an A instance or a subclass of it, but only if that subclass involves implementation of its parent. See this post.
  • private A() — That means that no other class than A itself and nested or encapsulating classes can call the constructor. It is often used to create singletons; with a static method the object is created:

    public class A {
    
        private static A instance;
    
        // Make constructor private
        private A() { }
    
        public static A getInstance() {
            // Create an instance if it is not yet created.
            if (A.instance == null) {
                A.instance = new A();
            }
            return A.instance;
        }
    }
    
  • A() — If an access modifier (like public or private) is omitted, it's called package-private. A package-private constructor can only be accessed by classes in the same package or subclasses of that class.

There is a table you might find useful:

             |Class | Package | Subclass | World
public       | Y    | Y       | Y        | Y
protected    | Y    | Y       | Y        | N
no modifier  | Y    | Y       | N        | N
private      | Y    | N       | N        | N

Note that no modifier means that you do not declare a modifier, that is, omit it.

Community
  • 1
  • 1
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • This is actually not entirely true. If the class `A` is nested within another class `B`, B can create an instance of `A` even if all of `A`'s constructors are private. [See here](http://ideone.com/eJqYIn) – Ingo Bürk Sep 25 '14 at 18:02
  • @IngoBürk That's correct; thanks for noticing. I've updated the answer. – MC Emperor Sep 25 '14 at 18:09
1

If you write a constructor with no access modifier:

public class MyClass {

    MyClass (int param) {
        // constructor code
    }

}

then the accessibility is package private, same as the accessibility of any method with no access modifier. (JLS 6.6.1: "A class member or constructor declared without an access modifier implicitly has package access.") So that's the default "type" (actually the default accessibility) of a constructor. In this example, the package-private constructor means that you can use new MyClass(10) from a class in the same package, but not outside the package.

You can add accessibility modifiers as with any other method: public, private, or protected. However, you cannot use other modifiers such as abstract, according to JLS 8.8.3.

If you make it private, then the constructor cannot be used outside the class (or, for a nested class, outside the top-level class that contains it), but it can be used inside the class. This means that your class can define a static factory method that isn't really a constructor, but is used to construct objects of the class:

public class MyClass {

    private MyClass(int param) { ... }

    public static MyClass CreateObject(String something) {
        // stuff
        return new MyClass(n);
    }

}

A similar idiom is also used by singleton classes, in which a method returns an instance of the class like a factory method, but also ensures only one instance is created.

The private constructor can also be used by other constructors:

public class MyClass {

    private MyClass(boolean isString, String stringParam, int intParam) {
        // stuff
    }

    public MyClass(String stringParam) {
        this(true, stringParam, -1);
    }

    public MyClass(int intParam) {
        this(false, null, intParam);
    }

}

If you declare a private constructor and don't use it anywhere in the class, that's an idiom often used to define a class whose only purpose is to provide static utility methods. You can call the static methods, but you can't create an instance of the class.

ajb
  • 31,309
  • 3
  • 58
  • 84
0

Yes you can change the type of the constructor in java. By default,the constructor is not public.For classes with default access modifier,constructor access is also default.For inner classes,the class can have any access modifier and so the constructor will also have the same access modifier unless we define a constructor with a different access modifier

private is used in case of singletons. Note if you do this then you cannot instantiate the class from outside the class.

For private constructor you can do something like which is called singletons

public class Foo {
    private Foo foo;
    private Foo(){} // default you may not see it in the editor but is provided by complier
    public static Foo getInstance(){ 
        if(foo==null)
           foo=new Foo();
        return foo;
    }
}
StackFlowed
  • 6,664
  • 1
  • 29
  • 45
  • By default,the constructor is not public.For classes with default access modifier,constructor access is also default.For inner classes,the class can have any access modifier and so the constructor will also have the same access modifier unless we define a constructor with a different access modifier – Kumar Abhinav Sep 25 '14 at 17:39
0

What is default type of constructor e.g. public/private/astract etc.? For default constructor It depends on the accessibility of the class , if class is declared with public then default constructor will be public , if class is declared protected then default constructor will be protected.
When I creating constructor as private its not allowing to access the class why? Because the constructor is not visible outside the class.

sol4me
  • 15,233
  • 5
  • 34
  • 34
  • But when we create a parametrized constructor then we call that one from outside the class. – Altmish-E-Azam Sep 25 '14 at 17:36
  • @Altmish when you are creating a constructor then it's upto you what type of access modifier(public/private/etc) you want to apply. – sol4me Sep 25 '14 at 17:38
0

if you have a single constructor and it is private, then it means nobody can create the object of your class. But there is a way to get the object of your class i.e., using singletone.

public class Foo {

    private static Foo foo;

    private Foo() { }

    public static Foo getInstance() {
        if (foo == null) {
            foo = new Foo();
        }
        return foo;
    }
}
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Manjunath
  • 169
  • 3
  • 1
    Your first sentence is not quite accurate. If you have **one** constructor and it's private, then an outside class cannot create an object of the class. But a class can have multiple constructors, where one of them is private and the others call it. – ajb Sep 25 '14 at 17:49