SlideShare une entreprise Scribd logo
1  sur  17
Objectives :
• What is constructor in java?
• Types of constructor in java
• Default constructor
• No-args constructor
• Parameterized constructor
What is constructor in java?
• Constructor are almost similar to methods except
for two things
• Name is same as the class name
• It has no return type
• Constructor in java is used to create the instance
of the class
Default constructor
• A default constructor is a constructor that
either has no parameters
• It has parameters, all the parameters have
default values.
Syntax:
Class classname
{
Test() {
// constructor body }
}
Example program:
class Main
{
private String name; // constructor Main()
{
System.out.println("Constructor Called:"); name = "Programiz";
}
static void main(String[] args) { // constructor is invoked while //
creating an object of the Main class Main obj = new Main();
System.out.println("The name is " + obj.name);
}
}
Output:
Constructor called: The
name is Programiz
No-args constructor
• 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.
Syntax:
Class classname
{
method name()// no arguments
{
------
}
}
Example program:
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);
}
}
Constructor is called Value of i: 5
Output:
Parameterized constructor
• Parameterized constructors in java are the constructors in a class
which have one or more than one arguments.
• Parameterized constructors are used to create user instances of
objects with user defined states.
• There can be more than one parameterized constructor in a class.
Syntax:
Class classname
{
method name(p1,p2)//two parameters passed
{
------
}
}
Example program
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
Thank you

Contenu connexe

Similaire à Constructors in JAva.pptx

Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
KavitaHegde4
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
myrajendra
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 

Similaire à Constructors in JAva.pptx (20)

UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Java Constructor
Java ConstructorJava Constructor
Java Constructor
 
BCA Class and Object (3).pptx
BCA Class and Object (3).pptxBCA Class and Object (3).pptx
BCA Class and Object (3).pptx
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
BCA Class and Object.pptx
BCA Class and Object.pptxBCA Class and Object.pptx
BCA Class and Object.pptx
 
Constructors.16
Constructors.16Constructors.16
Constructors.16
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
Constructor oopj
Constructor oopjConstructor oopj
Constructor oopj
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructor
ConstructorConstructor
Constructor
 
Constructor
ConstructorConstructor
Constructor
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 

Plus de V.V.Vanniaperumal College for Women

Plus de V.V.Vanniaperumal College for Women (20)

Control Memory.pptx
Control Memory.pptxControl Memory.pptx
Control Memory.pptx
 
ADDRESSING MODES.pptx
ADDRESSING MODES.pptxADDRESSING MODES.pptx
ADDRESSING MODES.pptx
 
Data_Transfer&Manupulation Instructions.pptx
Data_Transfer&Manupulation Instructions.pptxData_Transfer&Manupulation Instructions.pptx
Data_Transfer&Manupulation Instructions.pptx
 
Timing & Control.pptx
Timing & Control.pptxTiming & Control.pptx
Timing & Control.pptx
 
Human Rights - 1.pptx
Human Rights - 1.pptxHuman Rights - 1.pptx
Human Rights - 1.pptx
 
Registers.pptx
Registers.pptxRegisters.pptx
Registers.pptx
 
Instruction Codes.pptx
Instruction Codes.pptxInstruction Codes.pptx
Instruction Codes.pptx
 
Features of Java.pptx
Features of Java.pptxFeatures of Java.pptx
Features of Java.pptx
 
JVM.pptx
JVM.pptxJVM.pptx
JVM.pptx
 
IS-Crypttools.pptx
IS-Crypttools.pptxIS-Crypttools.pptx
IS-Crypttools.pptx
 
IS-Delibrate software attacks.pptx
IS-Delibrate software attacks.pptxIS-Delibrate software attacks.pptx
IS-Delibrate software attacks.pptx
 
IS-Nature of forces.ppt
IS-Nature of forces.pptIS-Nature of forces.ppt
IS-Nature of forces.ppt
 
IS-cryptograpy algorithms.pptx
IS-cryptograpy algorithms.pptxIS-cryptograpy algorithms.pptx
IS-cryptograpy algorithms.pptx
 
IS-Types of IDPSs.pptx
IS-Types of IDPSs.pptxIS-Types of IDPSs.pptx
IS-Types of IDPSs.pptx
 
IS-honeypot.pptx
IS-honeypot.pptxIS-honeypot.pptx
IS-honeypot.pptx
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
M-coloring.pptx
M-coloring.pptxM-coloring.pptx
M-coloring.pptx
 
storm.ppt
storm.pptstorm.ppt
storm.ppt
 
storm for RTA.pptx
storm for RTA.pptxstorm for RTA.pptx
storm for RTA.pptx
 
Yarn.ppt
Yarn.pptYarn.ppt
Yarn.ppt
 

Dernier

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 

Dernier (20)

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Constructors in JAva.pptx

  • 1.
  • 2. Objectives : • What is constructor in java? • Types of constructor in java • Default constructor • No-args constructor • Parameterized constructor
  • 3. What is constructor in java? • Constructor are almost similar to methods except for two things • Name is same as the class name • It has no return type • Constructor in java is used to create the instance of the class
  • 4.
  • 5. Default constructor • A default constructor is a constructor that either has no parameters • It has parameters, all the parameters have default values.
  • 7. Example program: class Main { private String name; // constructor Main() { System.out.println("Constructor Called:"); name = "Programiz"; } static void main(String[] args) { // constructor is invoked while // creating an object of the Main class Main obj = new Main(); System.out.println("The name is " + obj.name); } }
  • 9. No-args constructor • 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.
  • 10. Syntax: Class classname { method name()// no arguments { ------ } }
  • 11. Example program: 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); } }
  • 12. Constructor is called Value of i: 5 Output:
  • 13. Parameterized constructor • Parameterized constructors in java are the constructors in a class which have one or more than one arguments. • Parameterized constructors are used to create user instances of objects with user defined states. • There can be more than one parameterized constructor in a class.
  • 14. Syntax: Class classname { method name(p1,p2)//two parameters passed { ------ } }
  • 15. Example program 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"); }
  • 16. Output java Programming Language Python Programming Language C Programming Language