Computer Application X CH 4
Computer Application X CH 4
Constructor: A constructor is a special member method with the same name as that of its
class and it is used to initialize the objects of that class type with a legal initial value.
It is invoked (called) automatically at the time of object creation.
But, method and constructor have many differences also. Such as-
Difference on Constructors Methods
Name Same as the class name. Other than class name.
Purpose (need) To initialize the new object. For any other purpose.
Return type No return type, not even void. void or a valid return type.
Invoke (call) Invoked (called) automatically at Invoked (called) when method call
the time of object creation. statement is found during execution.
Modifier Modifiers (static) are not used. Modifiers are used.
Note: If a constructor is not defined in a class, compiler supplies the default constructor.
Syntax of defining constructor is same as method but modifier and return type are not
used. It is advised that access specifier should be public to access the object in another
class.
Computer Applications Notes Volume II (Class X) for ICSE examinations 2022 by Hem Sir Page 12
// Non-Parameterized (default) constructor
public Student()
{
studentId = 0;
marks = 0;
}
// member methods
public void display( )
{
System.out.println(“Student Id ” + studentId);
System.out.println(“Marks ” + marks);
}
// main method
public static void main (String args[])
{
Student stud = new Student();
stud.display();
}
}
Computer Applications Notes Volume II (Class X) for ICSE examinations 2022 by Hem Sir Page 13
// main method
public static void main (String args[])
{
Student stud = new Student(1, 97);
stud.display();
}
}
// Parameterized constructor
public Student(int s, int m)
{
studentId = s;
marks = m;
}
// copy constructor
public Student(Student ob)
{
studentId = ob.studentId;
marks = ob.marks;
}
Computer Applications Notes Volume II (Class X) for ICSE examinations 2022 by Hem Sir Page 14
// member methods
public void display( )
{
System.out.println(“Student Id ” + studentId);
System.out.println(“Marks ” + marks);
}
// main method
public static void main (String args[])
{
Student stud1 = new Student(); //default constructor
stud1.display();
Student stud2 = new Student(2, 95); // parameterized constructor
stud2.display();
Student stud3 = new Student(stud2); // copy constructor
Stud3.display();
}
}
this keyword: The keyword this is a reference that refers to the currently-calling object
in an instance method or constructor.
When a member method is called, it is automatically passed a hidden argument
that is a reference to the object that invoked the method / constructor. This reference is
called this.
The this reference can be implicit (java automatic uses this) or explicit (need to
write this keyword forcefully).
Note:- The this keyword solves the ambiguity of similar variable names. If the names of
instance variables and the local variables are different then this will not be required.
Computer Applications Notes Volume II (Class X) for ICSE examinations 2022 by Hem Sir Page 15
// Parameterized constructor using this keyword
public Student(int s, int marks)
{
studentId = s; // implicit use of this
this.marks = marks; // explicit use of this
}
// member methods
public void display( )
{
System.out.println(“Student Id ” + studentId);
System.out.println(“Marks ” + marks);
}
// main method
public static void main (String args[])
{
Student stud = new Student(3, 96);
stud.display();
}
}
Computer Applications Notes Volume II (Class X) for ICSE examinations 2022 by Hem Sir Page 16
// constructors
public Student(int s, int m)
{
studentId = s;
marks = m;
System.out.println(“ Constructor having two parameters”);
}
public Student(int s)
{
this(s, 0); // calling another constructor that is declared above
System.out.println(“ Constructor having single parameter”);
}
// member methods
public void display( )
{
System.out.println(“Student Id ” + studentId);
System.out.println(“Marks ” + marks);
}
// main method
public static void main (String args[])
{
Student stud = new Student(4);
stud.display();
}
}
Computer Applications Notes Volume II (Class X) for ICSE examinations 2022 by Hem Sir Page 17