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?
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?
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.
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.
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;
}
}
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.
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;
}
}