0% found this document useful (0 votes)
34 views5 pages

Experiment 6

1) Constructors are special methods in Java that are used to initialize objects. They are called when an object is created. 2) There are two types of constructors: default constructors that take no parameters, and parameterized constructors that allow passing parameters to initialize object attributes. 3) The "this" keyword can be used to call one constructor from another, allowing constructors to be chained together to share initialization code.

Uploaded by

69bunt Y
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)
34 views5 pages

Experiment 6

1) Constructors are special methods in Java that are used to initialize objects. They are called when an object is created. 2) There are two types of constructors: default constructors that take no parameters, and parameterized constructors that allow passing parameters to initialize object attributes. 3) The "this" keyword can be used to call one constructor from another, allowing constructors to be chained together to share initialization code.

Uploaded by

69bunt Y
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/ 5

Experiment No.

: 06

Title: Program to define class and constructors. Demonstrate constructors with this keyword.

Theory:

In Java, a constructor is a block of codes similar to the method. It is called when an


instance of the class is created. At the time of calling the constructor, memory for the object
is allocated in the memory.

It is a special type of method which is used to initialize the object. Every time an
object is created using the new() keyword, at least one constructor is called. It calls a default
constructor if there is no constructor available in the class. In such case, Java compiler
provides a default constructor by default.

Rules for creating Java constructor

There are two rules defined for the constructor.

• Constructor name must be the same as its class name


• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and synchronized

Types of Java constructors

There are two types of constructors in Java:

1. Default constructor (no-arg constructor)


2. Parameterized constructor

1. Java Default Constructor

A constructor is called "Default Constructor" when it doesn't have any parameter.

Syntax of default constructor:

<class_name>(){}

Program 1: Java Program to create and call a default constructor.

//Java Program to create and call a default constructor


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

Bike is created

The default constructor is used to provide the default values to the object like 0, null,
etc., depending on the type.

Program 2:

//Let us see another example of default constructor which displays


the default values
class Student3{
int id;
String name;

//method to display the value of id and name


void display(){
System.out.println(id+" "+name);
}

public static void main(String args[]){


//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
}
}

Output

0 null
0 null

Java Parameterized Constructor


A constructor which has a specific number of parameters is called a parameterized
constructor. The parameterized constructor is used to provide different values to distinct
objects. However, you can provide the same values also.
Program 3:

class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n) {
id = i; name = n;
}
//method to display the values
void display(){
System.out.println(id+" "+name);
}

public static void main(String args[]){


//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}

Output

111 Karan
222 Aryan

• “this” keyword in Java

There can be a lot of usage of Java this keyword. In Java, this is a reference
variable that refers to the current object. The this() constructor call can be used to invoke the
current class constructor. It is used to reuse the constructor. In other words, it is used for
constructor chaining.
Program 4: Calling default constructor from parameterized constructor

public class A {
A(){
System.out.println("hello a");
}
A(int x){
this();
System.out.println(x);
}
}
class TestThis5{
public static void main(String args[]){ A a=new A(10);
}
}

Output

hello a
10

Program 5: Calling parameterized constructor from default constructor

public class A {
A(){
this(5);
System.out.println("hello a");
}
A(int x){
System.out.println(x);
}
}
class TestThis6{
public static void main(String args[]){
A a=new A();
}
}

Output

5
hello a
Real usage of this() constructor call
The this() constructor call should be used to reuse the constructor from the
constructor. It maintains the chain between the constructors i.e. it is used for constructor
chaining. Let's see the example given below that displays the actual use of this keyword.

Program 6:

class Student{
int rollno;
String name,course;
float fee;

Student(int rollno,String name,String course){


this.rollno=rollno;
this.name=name;
this.course=course;
}

Student(int rollno,String name,String course,float fee){


//reusing constructor this.fee=fee;
this(rollno,name,course);
}

void display(){
System.out.println(rollno+" "+name+" "+course+" "+fee);}
}

class TestThis7{
public static void main(String args[]){
Student s1=new Student(111,"ankit","java");
Student s2=new Student(112,"sumit","java",6000f);
s1.display();
s2.display();
}
}

Output

111 ankit java 0.0


112 sumit java 6000f

Conclusion: Hence we have studied the use of constructors in java.

You might also like