What are Constructors in Java
A constructor creates an Object of the class that it is in by initializing all the instance variables and creating a place in memory to hold the Object.
We can’t make a new object with out invoking a constructor.
* In fact, you can’t make a new object without invoking not just the constructor of the objects actual class type but also the constructor of each of its super class.
Rules for Constructors
- The constructor name must match the name of the class.
- Constructor must not have a return type.
- If you don’t type a constructor into your class code, a default constructor will be automatically generated by the compiler.
- The default constructor is always a no-argument constructor.
- The compiler will not provide a Default constructor if your class contains any other constructor.
- Every constructor has its first statement, either a call to an overloaded constructor (this()) or a call to the super class constructor (Super()).
- If you type a constructor in your class and you don’t type in the call to super() or a call to this(), the compiler will insert a no- argument call to super(), as the first statement in the constructor.
- A call to super() can be either a no-argument call or can include arguments.
- A no-argument constructor is not necessarily the default (i.e., compiler – supplied) constructor, although the default constructor is always a no-argument constructor.
- You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs.
- Only static variables and methods can be accessed as part of the call to super() or this().
- Abstract classes have constructors, and those constructors are always called when a concrete subclass is instantiated.
- Interfaces do not have constructors.
Syntax for creating a constructor
There's only one syntax for creating a constructor in Java — a method with the same name as the class and no return type.
} // constructor in its most generic form
myClass(int n) //Argument constructor
Access Modifiers in Java
Java provides mechanisms for access control, to prevent the users of a package or class from depending on unnecessary details of the implementation of that package or class. Access control applies to qualified access and to the invocation of constructors by class instance creation expressions, explicit constructor invocations, and the method newInstance() of class Class.
- Access is controlled using the modifiers public, protected, and private. There are several rules to determine accessibility of an entity:
- If a class or interface type is declared public, then it may be accessed by any Java code that can access the package in which it is declared. If a class or interface type is not declared public, then it may be accessed only from within the package in which it is declared
- A member (field or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
- If the member or constructor is declared public, then access is permitted. All Members of interfaces are implicitly public.
- Otherwise, if the member or constructor is declared protected, then access is permitted only when one of the following is true:
- Access to the member or constructor occurs from within the package containing the class in which the protected member is declared.
- Access occurs within a subclass of the class in which the protected member is declared.
- Otherwise, if the member or constructor is declared private, then access is permitted only when it occurs from within the class in which it is declared.
- Otherwise, we say there is default access, which is permitted only when the access occurs from within the package in which the type is declared.
What are the allowed Access modifiers for constructors in java.
The possible access modifiers in Java are public, private, protected.
Constructor with private access modifier
A private constructor means only code within the class itself can instantiate an object of that type, so if the private-constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from with in the class.
public class MyClass
// Private constructor; this cannot be called outside the class
public static MyClass newInstance()
return new MyClass();
// Public static method //that calls the private constructor; this method
// can be called from other classes to get an
//instance of MyClass