Java Constructor
Saumya Som
BWU/BCA/17/207
BCA-C (4th Semester)
Teacher- Aryabhatta Ganguly
CONTENTS
1 Java Constructor
Rules for Java Constructor
Types of Java Constructor
2
3
Difference between constructors
and methods4
1
Java
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. It can be used to set
initial values for object attributes
2
Rules for
Java Constructor
The constructor name must match
the name of the class
The default constructor is
ALWAYS a no-arg constructor
Constructors can use any access
modifier, including private
Constructors must not have a
return type.
3
Types of Java
Constructor M A T R I X
01
02
No-Argument
Constructor
Parameterized
constructor
4
M A T R I X02
01
A constructor that
have no parameter
is known as default
constructor.
Syntax of default
constructor:
<class_name>(){}
Default Constructor class Bike1
{
//creating a default constructor
Bike1()
{
System.out.println("Bike is created");
}
//main method
public static void main(String args[])
{
//calling a default constructor
Bike1 b=new Bike1();
}
}
Exampl
e :
Output : Bike is created
5
M A T R I X02
01
A constructor that
have parameters is
known as
parameterized
constructor.
Parameterized
constructor is used
to provide different
values to the
distinct objects
Parameterized
constructor
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Exampl
e :
Output : 111 Karan
222 Aryan
6
Difference Between
Constructor and Method M A T R I X
Constructor are
used to initialize
the state of object
Constructor must
not have return
type
Constructor compil
er provide default
constructor .
Method is expose
the behaviour of
object.
Method must have
return type.
Method compiler
does't provide.
1
2
3
1
2
3
Java Constructors

More Related Content

PPTX
Methods and constructors in java
PPTX
Constructor in java
PPTX
Constructor in java
PDF
Java String
PPTX
This keyword in java
PPTX
PPS
Java Exception handling
PPT
Abstract class in java
Methods and constructors in java
Constructor in java
Constructor in java
Java String
This keyword in java
Java Exception handling
Abstract class in java

What's hot (20)

PPTX
Control flow statements in java
PPTX
Classes, objects in JAVA
PPTX
Java string handling
PDF
Enumeration in Java Explained | Java Tutorial | Edureka
PPTX
Static Members-Java.pptx
PPTX
Abstract Class Presentation
PPTX
Java exception handling
PDF
Java 8 Default Methods
PDF
Java IO
PPT
Method overriding
PPT
Introduction to method overloading &amp; method overriding in java hdm
PDF
Collections In Java
PPTX
Constructor in java
PPT
Java static keyword
PDF
PDF
Method Overloading In Java
PPTX
Arrays,string and vector
PPTX
Strings in Java
PPT
Basic of Multithreading in JAva
PPTX
Python strings presentation
Control flow statements in java
Classes, objects in JAVA
Java string handling
Enumeration in Java Explained | Java Tutorial | Edureka
Static Members-Java.pptx
Abstract Class Presentation
Java exception handling
Java 8 Default Methods
Java IO
Method overriding
Introduction to method overloading &amp; method overriding in java hdm
Collections In Java
Constructor in java
Java static keyword
Method Overloading In Java
Arrays,string and vector
Strings in Java
Basic of Multithreading in JAva
Python strings presentation
Ad

Similar to Java Constructors (20)

PPTX
Constructor oopj
PPTX
Constructors in java
PDF
Constructors in Java (2).pdf
PPTX
Constructor in java
PPTX
A constructor in Java is a special method that is used to initialize objects
PPTX
Constructors in java
PPTX
constructors.pptx
PPTX
Java ConstructorsPPT.pptx
PDF
Constructors In Java – Unveiling Object Creation
PPTX
CONSTRUCTORS.pptx
PPTX
Constructors in JAva.pptx
PPT
Constructors
PDF
Java constructors and types with examples
PPTX
UNIT 3- Java- Inheritance, Multithreading.pptx
PPTX
Definning class.pptx unit 3
PPTX
Java constructors
PPTX
Java constructors
PPT
java Methods and data Constructors (2).ppt
PPTX
object oriented programming CONSTRUCTORS.pptx
PDF
Constructor in Java - ITVoyagers
Constructor oopj
Constructors in java
Constructors in Java (2).pdf
Constructor in java
A constructor in Java is a special method that is used to initialize objects
Constructors in java
constructors.pptx
Java ConstructorsPPT.pptx
Constructors In Java – Unveiling Object Creation
CONSTRUCTORS.pptx
Constructors in JAva.pptx
Constructors
Java constructors and types with examples
UNIT 3- Java- Inheritance, Multithreading.pptx
Definning class.pptx unit 3
Java constructors
Java constructors
java Methods and data Constructors (2).ppt
object oriented programming CONSTRUCTORS.pptx
Constructor in Java - ITVoyagers
Ad

Recently uploaded (20)

PPTX
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
PPT
Module_1_Lecture_1_Introduction_To_Automation_In_Production_Systems2023.ppt
PDF
Software defined netwoks is useful to learn NFV and virtual Lans
PDF
Engineering Solutions for Ethical Dilemmas in Healthcare (www.kiu.ac.ug)
PDF
CB Công Nghiệp Slide .dh bách khoa đà nẵng
PDF
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
PPTX
1. Effective HSEW Induction Training - EMCO 2024, O&M.pptx
PPT
Unit - I.lathemachnespct=ificationsand ppt
PPTX
Research Writing, Mechanical Engineering
PDF
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
PDF
PhD defense presentation in field of Computer Science
PDF
ANTIOXIDANT AND ANTIMICROBIAL ACTIVITIES OF ALGERIAN POPULUS NIGRA L. BUDS EX...
PDF
IAE-V2500 Engine for Airbus Family 319/320
PPTX
highway-150803160405-lva1-app6891 (1).pptx
PPTX
Cloud Security and Privacy-Module-1.pptx
PDF
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
PDF
Performance, energy consumption and costs: a comparative analysis of automati...
PDF
Module 1 part 1.pdf engineering notes s7
PPTX
Soft Skills Unit 2 Listening Speaking Reading Writing.pptx
PPTX
240409 Data Center Training Programs by Uptime Institute (Drafting).pptx
22ME926Introduction to Business Intelligence and Analytics, Advanced Integrat...
Module_1_Lecture_1_Introduction_To_Automation_In_Production_Systems2023.ppt
Software defined netwoks is useful to learn NFV and virtual Lans
Engineering Solutions for Ethical Dilemmas in Healthcare (www.kiu.ac.ug)
CB Công Nghiệp Slide .dh bách khoa đà nẵng
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
1. Effective HSEW Induction Training - EMCO 2024, O&M.pptx
Unit - I.lathemachnespct=ificationsand ppt
Research Writing, Mechanical Engineering
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
PhD defense presentation in field of Computer Science
ANTIOXIDANT AND ANTIMICROBIAL ACTIVITIES OF ALGERIAN POPULUS NIGRA L. BUDS EX...
IAE-V2500 Engine for Airbus Family 319/320
highway-150803160405-lva1-app6891 (1).pptx
Cloud Security and Privacy-Module-1.pptx
THE PEDAGOGICAL NEXUS IN TEACHING ELECTRICITY CONCEPTS IN THE GRADE 9 NATURAL...
Performance, energy consumption and costs: a comparative analysis of automati...
Module 1 part 1.pdf engineering notes s7
Soft Skills Unit 2 Listening Speaking Reading Writing.pptx
240409 Data Center Training Programs by Uptime Institute (Drafting).pptx

Java Constructors

  • 1. Java Constructor Saumya Som BWU/BCA/17/207 BCA-C (4th Semester) Teacher- Aryabhatta Ganguly
  • 2. CONTENTS 1 Java Constructor Rules for Java Constructor Types of Java Constructor 2 3 Difference between constructors and methods4
  • 3. 1 Java 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. It can be used to set initial values for object attributes
  • 4. 2 Rules for Java Constructor The constructor name must match the name of the class The default constructor is ALWAYS a no-arg constructor Constructors can use any access modifier, including private Constructors must not have a return type.
  • 5. 3 Types of Java Constructor M A T R I X 01 02 No-Argument Constructor Parameterized constructor
  • 6. 4 M A T R I X02 01 A constructor that have no parameter is known as default constructor. Syntax of default constructor: <class_name>(){} Default Constructor class Bike1 { //creating a default constructor Bike1() { System.out.println("Bike is created"); } //main method public static void main(String args[]) { //calling a default constructor Bike1 b=new Bike1(); } } Exampl e : Output : Bike is created
  • 7. 5 M A T R I X02 01 A constructor that have parameters is known as parameterized constructor. Parameterized constructor is used to provide different values to the distinct objects Parameterized constructor class Student4{ int id; String name; //creating a parameterized constructor Student4(int i,String n){ id = i; name = n; } //method to display the values void display(){System.out.println(id+" "+name);} public static void main(String args[]){ //creating objects and passing values Student4 s1 = new Student4(111,"Karan"); Student4 s2 = new Student4(222,"Aryan"); //calling method to display the values of object s1.display(); s2.display(); } } Exampl e : Output : 111 Karan 222 Aryan
  • 8. 6 Difference Between Constructor and Method M A T R I X Constructor are used to initialize the state of object Constructor must not have return type Constructor compil er provide default constructor . Method is expose the behaviour of object. Method must have return type. Method compiler does't provide. 1 2 3 1 2 3