0% found this document useful (0 votes)
80 views4 pages

Constructors in Java

Constructors in Java are special methods that are used to initialize objects. There are two types of constructors: default constructors that require no parameters, and parameterized constructors that allow passing parameters during object creation. Constructors must have the same name as the class and cannot have an explicit return type. A copy constructor is a parameterized constructor that is used to copy the properties of one object to another.

Uploaded by

Vani Mittal
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)
80 views4 pages

Constructors in Java

Constructors in Java are special methods that are used to initialize objects. There are two types of constructors: default constructors that require no parameters, and parameterized constructors that allow passing parameters during object creation. Constructors must have the same name as the class and cannot have an explicit return type. A copy constructor is a parameterized constructor that is used to copy the properties of one object to another.

Uploaded by

Vani Mittal
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/ 4

Constructors in Java

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


1.Constructor name must be the same as its class name
2.A Constructor must have no explicit return type
3.A Java constructor cannot be abstract, static, final, and synchronized
Types of Java constructors Example of parameterized constructor
There are two types of constructors in
class Bike1{  
Java:
creating a default constructor  
1.Default constructor (no-arg
Bike1()
constructor)
{ System.out.println ("Bike is created");}
2.Parameterized constructor
Example of default constructor Bike1(int speed)
{ System.out.println ("Bike speed is :“ + speed + “km/hr”);}
}
class Bike1{   Class USE {  
creating a default constructor   public static void main(String args[]){  
Bike1() //calling a default constructor  
{ System.out.println ("Bike is created"); Bike1 b=new Bike1(80);  
} }  
} }  
Class USE {  
public static void main(String args[]){  
//calling a default constructor   Comment –
Bike1 b=new Bike1();  
}   If parametrized constructor is declared for a class. Then it is
}   mandatory to declare default constructor for the same class.
Else it will show compile-time error.
Copy constructor (Parameterized constructor)
It is same as parametrized constructor. But in this constructor object of a class is passed. And it is
used to copy the properties of one object to another object.
class Box {
int length, width, height;
Box() // default constructor
{ length = 10;
width = 10;
height = 10;
} Output –
Box (Box ob, Box ob1) // copy constructor
{ ob = ob1; ------------------------------------------------- 1 Volume = 8000
}
int volume()
Because the length, width and height values
{ return length*width*height; associated with object b2 is copied to object
} b1.
}
public class usebox
{ public static void main(String args[])
{ Box b1 = new Box();
Box b2 = new Box();
b2.length = 20; b2.width = 20; b2. height = 20;
Box b3 = new Box(b1, b2);
System.out.println ("Volume = "+ b1.volume());
}
}

You might also like