Java Class 5
Java Class 5
synchronized
Parameterized constructor
Java Default Constructor
<class_name>()
{
}
//Java Program to create and call a default construc
tor
class Bike1{
//creating a default constructor
Bike1()
{
System.out.println("Bike is created");
}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
}
}
Rule: If there is no constructor in a class, compiler automatically creates
a default constructor.
Q) What is the purpose of a default constructor?
The Java compiler provides a default The method is not provided by the
constructor if you don't have any compiler in any case.
constructor in a class.
The constructor name must be same as The method name may or may not be
the class name. same as the class name.
Java Copy Constructor
There is no copy constructor in Java. However, we can copy the
values from one object to another like copy constructor in C++.