SlideShare a Scribd company logo
Constructors in Java
Mrs.S.KAVITHA
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER
SCIENCE
SRI SARADA NIKETAN COLLEGE OF
SCIENCE FOR WOMEN,KARUR
• 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.
• It can be used to set initial values for object
attributes.
• 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.
how Constructors are Different From Methods in
Java?
• Constructors must have the same name as the
class within which it is defined it is not
necessary for the method in Java.
• Constructors do not return any type while
method(s) have the return type or void if does
not return any value.
• Constructors are called only once at the time
of Object creation while method(s) can be
called any number of times.
syntax for the constructor
class Geek
{ .......
// A Constructor
Geek()
{ } .......
} // We can create an object of the above class //
using the below statement. This statement //
calls above constructor.
Geek obj = new Geek();
• Each time an object is created using
a new() keyword, at least one constructor (it could
be the default constructor) is invoked to assign initial
values to the data members of the same class.
Rules for writing constructors are as follows:
• The constructor(s) of a class must have the same
name as the class name in which it resides.
• A constructor in Java can not be abstract, final, static,
or Synchronized.
• Access modifiers can be used in constructor
declaration to control its access i.e which other class
can call the constructor.
Types of Constructors in Java
Now is the correct time to discuss the types of
the constructor, so primarily there are two
types of constructors in java:
• No-argument constructor
• Parameterized Constructor
• Default Constructor
No-argument constructor:
• A constructor that has no parameter is
known as the No-argument or Zero argument
constructor.
• If we don’t define a constructor in a class,
then the compiler creates a constructor(with
no arguments) for the class.
• And if we write a constructor with arguments
or no arguments then the compiler does not
create a default constructor.
// no-argument constructor
import java.io.*;
class Geek {
int num;
String name;
// this would be invoked while an object
// of that class is created.
Geek() { System.out.println("Constructor called"); }}
class GFG {
public static void main(String[] args)
{
// this would invoke default constructor.
Geek geek1 = new Geek();
// Default constructor provides the default
// values to the object like 0, null
System.out.println(geek1.name);
System.out.println(geek1.num);
}}
Output
Constructor called
null 0
Parameterized Constructor
• A constructor that has parameters is known as
parameterized constructor.
• If we want to initialize fields of the class with
our own values, then use a parameterized
constructor.
import java.io.*;
class Geek {
// data members of the class.
String name;
int id;
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
Geek geek1 = new Geek("avinash", 68);
System.out.println("GeekName :" + geek1.name + " and GeekId :" + geek1.id);
}
}
Output
GeekName :avinash and GeekId :68
Default Constructor
• A constructor that has no parameters is known as
default the constructor.
• A default constructor is invisible. And if we write a
constructor with arguments or no arguments then the
compiler does not create a default constructor. It is
taken out.
• It is being overloaded and called a parameterized
constructor.
• The default constructor changed into the
parameterized constructor.
• But Parameterized constructor can’t change the
default constructor.
import java.io.*;
class GFG {
GFG() { System.out.println("Default constructor"); }
public static void main(String[] args)
{
GFG hello = new GFG();
}
}
Output
Default constructor
Constructor overloading in Java
• In Java, we can overload constructors like
methods.
• The constructor overloading can be defined
as the concept of having more than one
constructor with different parameters so that
every constructor can perform a different task.
public class Student {
//instance variables of the class
int id;
String name;
Student(){
System.out.println("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
//object creation
Student s = new Student();
System.out.println("nDefault Constructor values: n");
System.out.println("Student Id : "+s.id + "nStudent Name : "+s.name);
System.out.println("nParameterized Constructor values: n");
Student student = new Student(10, "David");
System.out.println("Student Id : "+student.id + "nStudent Name : "+student.name);
}
}
Output:
this a default constructor
Default Constructor values:
Student Id : 0
Student Name : null
Parameterized Constructor values:
Student Id : 10
Student Name : David

More Related Content

PPTX
OOP's Part 1
PPTX
Constructors in java
PDF
Constructors in Java (2).pdf
PPTX
Constructors in java
PPTX
Definning class.pptx unit 3
PDF
Chapter 7 - Constructors.pdf
PPTX
Chapter 7 - Constructors.pptx
PPTX
Constructors in JAva.pptx
OOP's Part 1
Constructors in java
Constructors in Java (2).pdf
Constructors in java
Definning class.pptx unit 3
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pptx
Constructors in JAva.pptx

Similar to A constructor in Java is a special method that is used to initialize objects (20)

PPTX
CONSTRUCTORS.pptx
PPTX
Constructor in java Presentation by Dawood Khan .pptx
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
PPTX
Java ConstructorsPPT.pptx
PPTX
Lec08 constructors
PPTX
Constructor overloading & method overloading
PPTX
Quick Interview Preparation for C# All Concepts
PPTX
Constructor in java
PPTX
BCA Class and Object.pptx
PPTX
Constructor oopj
PPTX
BCA Class and Object (3).pptx
PPTX
Constructor in java
PPTX
Methods and constructors in java
PPTX
CST 203 Lecture 7.pptx
PPTX
object oriented programming CONSTRUCTORS.pptx
PPTX
Constructor and Destructor
PPTX
Constructor destructor.ppt
PPTX
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPTX
Java Constructor
CONSTRUCTORS.pptx
Constructor in java Presentation by Dawood Khan .pptx
UNIT 3- Java- Inheritance, Multithreading.pptx
Java ConstructorsPPT.pptx
Lec08 constructors
Constructor overloading & method overloading
Quick Interview Preparation for C# All Concepts
Constructor in java
BCA Class and Object.pptx
Constructor oopj
BCA Class and Object (3).pptx
Constructor in java
Methods and constructors in java
CST 203 Lecture 7.pptx
object oriented programming CONSTRUCTORS.pptx
Constructor and Destructor
Constructor destructor.ppt
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Java Constructor
Ad

More from Kavitha S (9)

PPTX
data link layer is the protocol layer in a program that handles the moving of...
PPTX
Java provides statements that can be used to control the flow of Java code
PPTX
When a break statement is encountered inside a loop, the loop is immediately ...
PPTX
a variable in Java must be a specified data type
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
The final keyword in java is used to restrict the user.
PPTX
the array, which stores a fixed-size sequential collection of elements of the...
PPTX
A class which is declared with the abstract keyword is known as an abstract c...
PPTX
How to create a two-dimensional array in java
data link layer is the protocol layer in a program that handles the moving of...
Java provides statements that can be used to control the flow of Java code
When a break statement is encountered inside a loop, the loop is immediately ...
a variable in Java must be a specified data type
Inheritance in Java is a mechanism in which one object acquires all the prope...
The final keyword in java is used to restrict the user.
the array, which stores a fixed-size sequential collection of elements of the...
A class which is declared with the abstract keyword is known as an abstract c...
How to create a two-dimensional array in java
Ad

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
Computing-Curriculum for Schools in Ghana
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Lesson notes of climatology university.
PPTX
Cell Structure & Organelles in detailed.
GDM (1) (1).pptx small presentation for students
Computing-Curriculum for Schools in Ghana
01-Introduction-to-Information-Management.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
human mycosis Human fungal infections are called human mycosis..pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Complications of Minimal Access Surgery at WLH
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pre independence Education in Inndia.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Microbial diseases, their pathogenesis and prophylaxis
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Supply Chain Operations Speaking Notes -ICLT Program
Lesson notes of climatology university.
Cell Structure & Organelles in detailed.

A constructor in Java is a special method that is used to initialize objects

  • 1. Constructors in Java Mrs.S.KAVITHA ASSISTANT PROFESSOR DEPARTMENT OF COMPUTER SCIENCE SRI SARADA NIKETAN COLLEGE OF SCIENCE FOR WOMEN,KARUR
  • 2. • 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. • It can be used to set initial values for object attributes. • 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.
  • 3. how Constructors are Different From Methods in Java? • Constructors must have the same name as the class within which it is defined it is not necessary for the method in Java. • Constructors do not return any type while method(s) have the return type or void if does not return any value. • Constructors are called only once at the time of Object creation while method(s) can be called any number of times.
  • 4. syntax for the constructor class Geek { ....... // A Constructor Geek() { } ....... } // We can create an object of the above class // using the below statement. This statement // calls above constructor. Geek obj = new Geek();
  • 5. • Each time an object is created using a new() keyword, at least one constructor (it could be the default constructor) is invoked to assign initial values to the data members of the same class. Rules for writing constructors are as follows: • The constructor(s) of a class must have the same name as the class name in which it resides. • A constructor in Java can not be abstract, final, static, or Synchronized. • Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.
  • 6. Types of Constructors in Java Now is the correct time to discuss the types of the constructor, so primarily there are two types of constructors in java: • No-argument constructor • Parameterized Constructor • Default Constructor
  • 7. No-argument constructor: • A constructor that has no parameter is known as the No-argument or Zero argument constructor. • If we don’t define a constructor in a class, then the compiler creates a constructor(with no arguments) for the class. • And if we write a constructor with arguments or no arguments then the compiler does not create a default constructor.
  • 8. // no-argument constructor import java.io.*; class Geek { int num; String name; // this would be invoked while an object // of that class is created. Geek() { System.out.println("Constructor called"); }} class GFG { public static void main(String[] args) { // this would invoke default constructor. Geek geek1 = new Geek(); // Default constructor provides the default // values to the object like 0, null System.out.println(geek1.name); System.out.println(geek1.num); }} Output Constructor called null 0
  • 9. Parameterized Constructor • A constructor that has parameters is known as parameterized constructor. • If we want to initialize fields of the class with our own values, then use a parameterized constructor.
  • 10. import java.io.*; class Geek { // data members of the class. String name; int id; Geek(String name, int id) { this.name = name; this.id = id; } } class GFG { public static void main(String[] args) { // This would invoke the parameterized constructor. Geek geek1 = new Geek("avinash", 68); System.out.println("GeekName :" + geek1.name + " and GeekId :" + geek1.id); } } Output GeekName :avinash and GeekId :68
  • 11. Default Constructor • A constructor that has no parameters is known as default the constructor. • A default constructor is invisible. And if we write a constructor with arguments or no arguments then the compiler does not create a default constructor. It is taken out. • It is being overloaded and called a parameterized constructor. • The default constructor changed into the parameterized constructor. • But Parameterized constructor can’t change the default constructor.
  • 12. import java.io.*; class GFG { GFG() { System.out.println("Default constructor"); } public static void main(String[] args) { GFG hello = new GFG(); } } Output Default constructor
  • 13. Constructor overloading in Java • In Java, we can overload constructors like methods. • The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.
  • 14. public class Student { //instance variables of the class int id; String name; Student(){ System.out.println("this a default constructor"); } Student(int i, String n){ id = i; name = n; } public static void main(String[] args) { //object creation Student s = new Student(); System.out.println("nDefault Constructor values: n"); System.out.println("Student Id : "+s.id + "nStudent Name : "+s.name); System.out.println("nParameterized Constructor values: n"); Student student = new Student(10, "David"); System.out.println("Student Id : "+student.id + "nStudent Name : "+student.name); } }
  • 15. Output: this a default constructor Default Constructor values: Student Id : 0 Student Name : null Parameterized Constructor values: Student Id : 10 Student Name : David