0% found this document useful (0 votes)
32 views6 pages

Computer Application X CH 4

The document discusses constructors in Java. Constructors are used to initialize objects and are invoked automatically at object creation. They can be parameterized or non-parameterized. Constructors can be overloaded and one constructor can call another using this(). The this keyword refers to the current object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Computer Application X CH 4

The document discusses constructors in Java. Constructors are used to initialize objects and are invoked automatically at object creation. They can be parameterized or non-parameterized. Constructors can be overloaded and one constructor can call another using this(). The this keyword refers to the current object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Constructors

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.

There are many similarities between method and constructor such as –


 Both are members of class.
 Both can be parameterized and non-parameterized.
 Both can be overloaded. etc.

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.

Types of constructors: Constructors can be divided into two major categories:


 Non-Parameterized (Default) constructor: A constructor that does not accept any
parameter and initialize object with a fixed value.
 Parameterized constructor: A constructor that receives parameters and initializes
objects with received values.

Example of class with a non-parameterized constructor:


public class Student
{
// data members
int studentId;
int marks;

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();
}
}

Example of class with a parameterized constructor:


public class Student
{ // data members
int studentId;
int marks;
// Parameterized constructor
public Student(int s, int m)
{
studentId = s;
marks = m;
}
// member methods
public void display( )
{
System.out.println(“Student Id ” + studentId);
System.out.println(“Marks ” + marks);
}

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();
}
}

Constructor overloading : Like methods constructors can also be overloaded. We can


create more than one constructor in a class having different signatures (parameters).
If a parameterized constructor receives a parameter of the same type as the class and
used to copy the object is known as copy constructor.
Example of constructor overloading :
public class Student
{
// data members
int studentId;
int marks;

// Non-Parameterized (default) constructor


public Student()
{
studentId = 0;
marks = 0;
}

// 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.

Example of this keyword:


public class Student
{
// data members
int studentId;
int marks;

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();
}
}

Temporary Instances: The explicit call to a constructor allows us to create a temporary


instance / temporary object. The temporary instances do not bear a name so it can be used
once, after that it is deleted.
For example: In above main method if we write as-
new Student(3, 96).display();
then we cannot use it (temporary object) in next line.

Calling a constructor inside another constructor (Chaining of constructors): We can use


this( ) to call another constructor of same class from within a constructor, but this call
should be the first statement within a constructor. For example-

public class Student


{ // data members
int studentId;
int marks;

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy