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

Constructor

A constructor in Java is a special method used to initialize objects when a class instance is created. There are two types of constructors: Default Constructor, which has no parameters or parameters with default values, and Parameterized Constructor, which accepts specific arguments to initialize object attributes. The document provides examples of both types of constructors in Java code.

Uploaded by

Bastin Rogers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Constructor

A constructor in Java is a special method used to initialize objects when a class instance is created. There are two types of constructors: Default Constructor, which has no parameters or parameters with default values, and Parameterized Constructor, which accepts specific arguments to initialize object attributes. The document provides examples of both types of constructors in Java code.

Uploaded by

Bastin Rogers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Constructor

What is Constructor?
A constructor in Java is a special method
that is used to initialize objects.
The constructor is called when an object of
a class is created.
Types

Default
Constructor
Parametrized Constructor
DEFAULT CONSTRUCTOR
 TheDefault Constructor is a constructor that either
has no parameters, or if it it has parameters all
parameters have default values. If there is no
Constructor in a class, Compiler automatically
creates a Default Constructor
Default Constructor
class Test
{
Test() //Default Constructor
{
System.out.println(“Default Constructor is Invoked”);
}
}
PARAMETRIZED CONSTRUCTOR

A constructor having a specific number of


parameters(arguments) is called a
parameterized constructor.
 The parameterized constructor is used to
provide different values to the objects, you can
also provide the same values.
class Student
{
int rollno;
String name;
void display()
{
System.out.println(rollno);
System.out.println(name);
}
public static void main(String...s)
{
Student ss = new Student();//creating object of Student class
ss.void();//calling method
}
}

You might also like