Java Constructors
BY MARCO ALZAMORA
What is a Constructor
• A CONSTRUCTOR IS A SPECIAL TYPE OF METHOD
THAT IS USED TO INITIALIZE AN OBJECT.
• THEY DO NOT CONTAIN ANY RETURN TYPE.
• THEY HAVE THE SAME AS THE CLASS.
Types of Constructors
Default Constructor (contains no arguments
Parameterized constructor (One or more
arguments)
Default Constructor
 Example:
Class Car {
Car()
{
System.out.println(“Constructor has been created”);
}
Public static void main(String args[])
{
Car c = new Car();
}
}
The keyword new
creates the Car object
and invokes the
constructor to initialize
the object created
Parameterized constructor
public class Student {
int stuId;
String stuName;
//parameterized constructor with two parameters
Student ( int id, String name ){
stuId= id;
stuName = name;
}
void info(){
System.out.println("Id: "+stuId+" Name: "+stuName);
}
public static void main(String args[]){
Employee obj1 = new Student(00015,“Smith");
Employee obj2 = new Student(00017,“Watson");
obj1.info();
obj2.info();
}
}
Two objects created: obj1, and
obj2
Constructor same name as
the class with two
parameters
Call the obj1, obj2 instances of the
class student
Void method of the
class Student
Difference between a constructor and
a method
Constructor Method
Constructor is used to initialize the state of an
object.
Method is used to expose the behavior of an
object.
Constructor must not have return type. Method must have a return type.
Constructor name must be same as the class
name.
Method name should not be the same as the class
name.
Constructor is invoked implicitly. Method is invoked implicitly.

Java constructors

  • 1.
  • 2.
    What is aConstructor • A CONSTRUCTOR IS A SPECIAL TYPE OF METHOD THAT IS USED TO INITIALIZE AN OBJECT. • THEY DO NOT CONTAIN ANY RETURN TYPE. • THEY HAVE THE SAME AS THE CLASS.
  • 3.
    Types of Constructors DefaultConstructor (contains no arguments Parameterized constructor (One or more arguments)
  • 4.
    Default Constructor  Example: ClassCar { Car() { System.out.println(“Constructor has been created”); } Public static void main(String args[]) { Car c = new Car(); } } The keyword new creates the Car object and invokes the constructor to initialize the object created
  • 5.
    Parameterized constructor public classStudent { int stuId; String stuName; //parameterized constructor with two parameters Student ( int id, String name ){ stuId= id; stuName = name; } void info(){ System.out.println("Id: "+stuId+" Name: "+stuName); } public static void main(String args[]){ Employee obj1 = new Student(00015,“Smith"); Employee obj2 = new Student(00017,“Watson"); obj1.info(); obj2.info(); } } Two objects created: obj1, and obj2 Constructor same name as the class with two parameters Call the obj1, obj2 instances of the class student Void method of the class Student
  • 6.
    Difference between aconstructor and a method Constructor Method Constructor is used to initialize the state of an object. Method is used to expose the behavior of an object. Constructor must not have return type. Method must have a return type. Constructor name must be same as the class name. Method name should not be the same as the class name. Constructor is invoked implicitly. Method is invoked implicitly.