0% found this document useful (0 votes)
1 views

constructor-java

A constructor is a special method used for initializing objects at the time of creation, sharing the same name as the class and not returning values. Java supports constructor overloading, allowing multiple constructors with different parameter lists for varied initialization. Additionally, while Java does not have a built-in copy constructor, object values can be copied using assignment or the clone() method.

Uploaded by

rishavthakkar02
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

constructor-java

A constructor is a special method used for initializing objects at the time of creation, sharing the same name as the class and not returning values. Java supports constructor overloading, allowing multiple constructors with different parameter lists for varied initialization. Additionally, while Java does not have a built-in copy constructor, object values can be copied using assignment or the clone() method.

Uploaded by

rishavthakkar02
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Constructor

Constructor
• Constructor is a special method that gets invoked “automatically” at the
time of object creation.
• Constructor is normally used for initializing objects with default values
unless different values are supplied.
• Constructor has the same name as the class name.
• Constructor cannot return values.
• A class can have more than one constructor as long as they have
different
signature (i.e., different input arguments syntax).
Defining a Constructor:
Example
public class Counter {
int CounterIndex;

// Constructor
public Counter()
{
CounterIndex = 0;
}
//Methods to update or
access counter
public void increase()
{
CounterIndex =
CounterIndex +
1;
}
public void decrease()
{
CounterIndex =
CounterIndex -
1;
}
int getCounterIndex()
{
return CounterIndex; 3
Defining a Constructor:
Example
public class Counter {
int CounterIndex;

// Constructor
public Counter()
{
CounterIndex = 0;
}
//Methods to update or
access counter
public void increase()
{
CounterIndex =
CounterIndex +
1;
}
public void decrease()
{
CounterIndex =
CounterIndex -
1;
}
int getCounterIndex()
{
return CounterIndex; 4
each statement and What is
the output ?
class MyClass {
public static void main(String args[])
{
Counter counter1 = new
Counter();
counter1.increase();
int a =
counter1.getCounterIndex();
counter1.increase();
int b =
counter1.getCounterIndex(); if
(a>b)
counter1.increase();
else
counter1.decrease();

System.out.println(counter1.get
CounterIndex());
5
}
Java parameterized
constructor
AWhy
constructor that have parameters is known as parameterized
use parameterized
constructor.
constructor?
Parameterized constructor is used to provide different values to the
distinct objects.

6
Constructor Overloading
 Constructor overloading is a technique in Java in which a class can have any
number of constructors that differ in parameter lists. The compiler
differentiates these constructors by taking into account the number of
parameters in the list and their type.

7
Multiple Constructors
 Sometimes want to initialize a
object in a number of different
ways, depending on circumstance.

 This can be supported by having


multiple constructors having
different input arguments.

8
Multiple Constructors
public class Circle {
public double x,y,r; //instance variables
// Constructors
public Circle(double centreX, double cenreY, double radius)
{ x = centreX; y = centreY; r = radius;
}
public Circle(double radius) { x=0; y=0; r = radius; }
public Circle() { x=0; y=0; r=1.0; }

//Methods to return circumference and area


public double circumference() { return 2*3.14*r; }
public double area() { return 3.14 * r * r; }
}

9
Initializing with
constructors
public class TestCircles {

public static void main(String args[]){


Circle circleA = new Circle( 10.0, 12.0,
20.0); Circle circleB = new
Circle(10.0);
Circle circleC = new Circle();
}
}
circleA = new Circle(10, 12, circleB = new circleC = new
20) Circle(10) Circle()

Centre = Centre =
(10,12) Centre = (0,0)
Radius = 20 (0,0) Radius = 1 1
Java Copy Constructor

 There is no copy constructor in java. But, we


can copy the values of one object to another
like copy constructor in C++.
There are many ways to copy the values of
one object into another in java. They are:
 By constructor

 By assigning the values of one object into

another
 By clone() method of Object class

11
constructor
 We can copy the values of one object into another
by assigning the objects values to another object. In
this case, there is no need to create the constructor.

12
Difference between constructor and
method in java

13

You might also like