0% found this document useful (0 votes)
14 views7 pages

L9 - Constructors

Uploaded by

suiiisimon0
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)
14 views7 pages

L9 - Constructors

Uploaded by

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

Constructors

❖ A constructor in Java is similar to a method that is invoked when


an object of the class is created.
❖ Unlike Java methods, a constructor has the same name as that of
the class and does not have any return type.
For example,
class Test {
Test() {
// constructor body
}
}

Here,
❖ Test() is a constructor. It has the same name as that of the class
and doesn't have a return type.

Example:
class Cons {
private String name;

// constructor
Cons() {
System.out.println("Constructor Called:");
name = "Prajesh";
}

public static void main(String[] args) {

// constructor is invoked while creating an object of the Cons class


Cons obj = new Cons();
System.out.println("The name is " + obj.name);
}
}
Output:
Constructor Called:
The name is Prajesh
Types of Constructor

In Java, constructors can be divided into three types:


1. No-Arg Constructor

2. Parameterized Constructor

3. Default Constructor

1. No-Arg Constructors:
❖ Similar to methods, a Java constructor may or may not have any
parameters (arguments).

❖ If a constructor does not accept any parameters, it is known as a


no-argument constructor.

For example,

private Constructor()
{
// body of the constructor
}
For example: Java Private No-arg Constructor
class Main {
int i;
// constructor with no parameter
private Main() {
i = 5;
System.out.println("Constructor is called");
}
public static void main(String[] args) {
// calling the constructor without any parameter
Main obj = new Main();
System.out.println("Value of i: " + obj.i);
}
}
Output:

Constructor is called
Value of i: 5

Example: Java Public no-arg Constructors

class Company {
String name;
// public constructor
public Company() {
name = "Prabha";
}
}
class Main {
public static void main(String[] args) {
// object is created in another class
Company obj = new Company();
System.out.println("Company name = " + obj.name);
}
}
Output:

Company name = Prabha


2. Parameterized Constructor:

❖ A Java constructor can also accept one or more parameters. Such


constructors are known as parameterized constructors
(constructors with parameters).

Example: Parameterized Constructor

class Main {
String languages;
// constructor accepting single value
Main(String lang) {
languages = lang;
System.out.println(languages + " Programming Language");
}
public static void main(String[] args) {
// call constructor by passing a single value
Main obj1 = new Main("Java");
Main obj2 = new Main("Python");
Main obj3 = new Main("C");
}
}
Output:

Java Programming Language


Python Programming Language
C Programming Language

In the above example,


❖ We have created a constructor named Main() .

❖ Here, the constructor takes a single parameter. Notice the


expression:

Main obj1 = new Main("Java");

❖ Here, we are passing the single value to the constructor.


❖ Based on the argument passed, the language variable is initialized
inside the constructor.

3. Default Constructor:

❖ If we do not create any constructor, the Java compiler


automatically creates a no-arg constructor during the
execution of the program. This constructor is called the default
constructor.

Example: Default Constructor

class Main {

int a;
boolean b;

public static void main(String[] args) {

// calls default constructor


Main obj = new Main();

System.out.println("Default Value:");
System.out.println("a = " + obj.a);
System.out.println("b = " + obj.b);
}
}
Output:

Default Value:
a = 0
b = false

❖ Here, we haven't created any constructors. Hence, the Java compiler


automatically creates the default constructor.

❖ The default constructor initializes any uninitialized instance variables


with default values.
Type Default Value

boolean false

byte 0

short 0

int 0

long 0L

char \u0000

float 0.0f

double 0.0d

object Reference null

Important Notes on Java Constructors

❖ Constructors are invoked implicitly when you instantiate objects.


❖ The two rules for creating a constructor are:
1. The name of the constructor should be the same as the class.
2. A Java constructor must not have a return type.
❖ If a class doesn't have a constructor, the Java compiler automatically
creates a default constructor during run-time.
❖ The default constructor initializes instance variables with default values.
For example, the int variable will be initialized to 0
❖ Constructortypes:
1. No-Arg Constructor - a constructor that does not accept any arguments
2. Parameterized constructor - a constructor that accepts arguments
3. Default Constructor - a constructor that is automatically created by the Java
compiler if it is not explicitly defined.
❖ A constructor cannot be abstract or static or final.
❖ A constructor can be overloaded but can not be overridden.
Constructors Overloading in Java:
❖ Similar to Java method overloading, we can also create two or more
constructors with different parameters. This is called constructor overloading.

Example: Constructor Overloading


class Main {
String language;
// constructor with no parameter
Main() {
this.language = "Java";
}

// constructor with a single parameter


Main(String language) {
this.language = language;
}

public void getName() {


System.out.println("Programming Language: " + this.language);
}

public static void main(String[] args) {


// call constructor with no parameter
Main obj1 = new Main();
// call constructor with a single parameter
Main obj2 = new Main("Python");
obj1.getName();
obj2.getName();
}
}
Output:

Programming Language: Java


Programming Language: Python

You might also like