What is the purpose of the default
constructor in java
class Bike1 {
Bike1() {
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
What is the purpose of the default
constructor in java
class Bike1 {
Bike1() {
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.
Your example provides a constructor,
Bike1(){System.out.println("Bike is created");}
which means you do not get a default
constructor. A default constructor is inserted if you do not provide any constructor. Finally, Bike1
is a no-args constructor with package level (or default) access permission and appears to display a message when an instance of Bike1
is created.
Default constructor means that when you don't create any constructor for your class, the compiler automatically creates a default constructor (with no parameters) to your class at the time of compilation.
In your example you created a constructor. The constructor does not create any objects, it initialize the object.
Default constructors allow you to create objects with known, default settings and behavior. If you call a constructor with arguments, you are creating a custom object. But calling the default constructor will create objects with identical properties every time it is used.
Typically, a default constructor with "no code" doesn't need any code; it already has all the information it needs to create the object.
Remember that default constructor and a constructor with no-args are different.
Since you are defining a constructor Bike1(){}
here, the default constructor will loose it's scope and will not be generated automatically.
The default constructor is the no-argument constructor automatically generated unless you define another constructor. It initialises any uninitialised fields to their default values... follow this link.. Java default constructor
Default constructor have no arguments(parameters) and constructor name is same as class name.It will invoke at the time of object creation.
Example:
class Display{
Display(){
System.out.println("Default Constructor");
}
}
class constructor{
public static void main(String args[]){
Display dis=new Display();
}
}
Output:
Default Constructor
Because when the time of object creation default constructor will invoke automatically.
First thing we need to know that default constructor and no argument constructor are both different things. The no argument constructor is one which we declare inside the class where we actually may or may not write the functionality. The default constructor is one which will be called after object creation. The main purpose of this is to initialize the attributes of the object with their default values.
Java compiler provides a default constructor by default if there is no constructor available in the class