SlideShare a Scribd company logo
1 of 12
JAVA
Topics for Today’s Session
Constructor
Difference between
Method and Constructor
Types of Constructors
Constructors
 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
 In Java, a constructor is a block of codes similar to the method.
It is called when an instance of the class is created.
 At the time of calling constructor, memory for the object is
allocated in the memory.
 Every time an object is created using the new() keyword, at
least one constructor is called.
Rules for creating Java constructor
 Constructor name must be the same as its class name
 A Constructor must have no explicit return type
 A Java constructor cannot be abstract, static, final, and
synchronized
Types of Java constructors
 There are two types of constructors in Java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor
Types
of
Constructors
Default Constructor
Parameterized
Constructor
 A constructor that have no parameter is known as default
constructor.
 Default constructor provides the default values to the object like 0,
null etc. depending on the type.
 If there is no constructor in a class, compiler automatically creates a
default constructor.
Syntax :
<class_name>(){ }
// Create a MyClass class
class MyClass {
int n; // Create a class attribute
// Create a class constructor for the MyClass class
public MyClass() {
n = 10; // Set the initial value for the class attribute n
}
public static void main(String[] args)
{
MyClass myObj = new MyClass(); // Create an object of class MyClass
// ( This will call the constructor)
System.out.println(myObj.n); // Print the value of n
}
}
// Outputs
10
Example of default constructor that displays the default values
class Student{
int id;
String name;
void display()
{ System.out.println(id+" "+name);
}
public static void main(String args[]){
Student s1=new Student();
Student s2=new Student();
s1.display();
s2.display();
} }
Output
0 null
0 null
Explanation:
In the above class,you are not creating any constructor so compiler
provides you a default constructor.Here 0 and null values are provided by
default constructor.
Parameterized constructor
 Constructors that have parameters is known as parameterized
constructor.
 Parameterized constructor is used to provide different values to
the distinct objects.
Example of parameterized constructor
/*In this example, we have created the constructor of Student class that
have two parameters. We can have any number of parameters in the
constructor. */
class Student {
int id;
String name;
Student (int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[])
{
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
s1.display();
s2.display();
} }
Difference between constructor and method in java
There are many differences between constructors and methods.
Constructor is used to
initialize the state of an object.
Method is used to expose
behaviour of an object.
Constructor must not have
return type.
Method must have return
type.
Constructor is invoked
implicitly.
Method is invoked explicitly.
The java compiler provides a
default constructor if you don't
have any constructor.
Method is not provided by
compiler in any case.
Constructor name must be
same as the class name.
Method name may or may
not be same as class name.
// Using Constructor
class Car {
int modYr;
String modName;
public Car(int y, String n) {
modYr = y;
modName = n;
}
public static void main(String[] args) {
Car myCar = new Car(1969, "Mustang");
System.out.println(myCar.modYr + " "
+ myCar.modName);
}
}
// Outputs
1969 Mustang
// Using Mathods
class Car {
int modYr;
String modName;
void getdata(int y, String n) {
modYr = y;
modName = n;
}
void show()
{
System.out.println(modYr);
System.out.println(modName);
}
public static void main(String[] args)
{
Car myCar = new Car( )
myCar.getdata(1969, "Mustang");
myCar.show();
}
}
// Outputs
1969
Mustang




Constructor in java

More Related Content

What's hot

Java package
Java packageJava package
Java packageCS_GDRCST
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Constructor in java
Constructor in javaConstructor in java
Constructor in javaHitesh Kumar
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Java exception handling
Java exception handlingJava exception handling
Java exception handlingBHUVIJAYAVELU
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 

What's hot (20)

Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java package
Java packageJava package
Java package
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java swing
Java swingJava swing
Java swing
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
 

Similar to Constructor in java

constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptxEpsiba1
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdfkumari36
 
Constructors in java
Constructors in javaConstructors in java
Constructors in javachauhankapil
 
Constructors In Java – Unveiling Object Creation
Constructors In Java – Unveiling Object CreationConstructors In Java – Unveiling Object Creation
Constructors In Java – Unveiling Object CreationGeekster
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructorchauhankapil
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop Samad Qazi
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloadinggarishma bhatia
 
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.pptxDrYogeshDeshmukh1
 
Constructors in java
Constructors in javaConstructors in java
Constructors in javasunilchute1
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its TypesMuhammad Hammad Waseem
 

Similar to Constructor in java (20)

Constructor oopj
Constructor oopjConstructor oopj
Constructor oopj
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
Constructors in Java (2).pdf
Constructors in Java (2).pdfConstructors in Java (2).pdf
Constructors in Java (2).pdf
 
Constructor
ConstructorConstructor
Constructor
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Constructors In Java – Unveiling Object Creation
Constructors In Java – Unveiling Object CreationConstructors In Java – Unveiling Object Creation
Constructors In Java – Unveiling Object Creation
 
Constructor&amp; destructor
Constructor&amp; destructorConstructor&amp; destructor
Constructor&amp; destructor
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Constructor
ConstructorConstructor
Constructor
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
 
BCA Class and Object.pptx
BCA Class and Object.pptxBCA Class and Object.pptx
BCA Class and Object.pptx
 
BCA Class and Object (3).pptx
BCA Class and Object (3).pptxBCA Class and Object (3).pptx
BCA Class and Object (3).pptx
 
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
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Constructors in JAva.pptx
Constructors in JAva.pptxConstructors in JAva.pptx
Constructors in JAva.pptx
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 

More from Madishetty Prathibha

More from Madishetty Prathibha (14)

Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - Introduction
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Structure of java program diff c- cpp and java
Structure of java program  diff c- cpp and javaStructure of java program  diff c- cpp and java
Structure of java program diff c- cpp and java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Types of datastructures
Types of datastructuresTypes of datastructures
Types of datastructures
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
Introduction to data structures (ss)
Introduction to data structures (ss)Introduction to data structures (ss)
Introduction to data structures (ss)
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
 
Java features
Java  features Java  features
Java features
 
Introduction of java
Introduction  of javaIntroduction  of java
Introduction of java
 

Recently uploaded

The State of AI in Insights and Research 2024: Results and Findings
The State of AI in Insights and Research 2024: Results and FindingsThe State of AI in Insights and Research 2024: Results and Findings
The State of AI in Insights and Research 2024: Results and FindingsRay Poynter
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Production of Monoclonal Antibodies by Hybridoma Technology.pptx
Production of Monoclonal Antibodies by Hybridoma Technology.pptxProduction of Monoclonal Antibodies by Hybridoma Technology.pptx
Production of Monoclonal Antibodies by Hybridoma Technology.pptxAnupkumar Sharma
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
PRINCIPLE & APPLICATIONS OF IMMUNO BLOTTING TECHNIQUES.pptx
PRINCIPLE & APPLICATIONS OF IMMUNO BLOTTING TECHNIQUES.pptxPRINCIPLE & APPLICATIONS OF IMMUNO BLOTTING TECHNIQUES.pptx
PRINCIPLE & APPLICATIONS OF IMMUNO BLOTTING TECHNIQUES.pptxAnupkumar Sharma
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 

Recently uploaded (20)

The State of AI in Insights and Research 2024: Results and Findings
The State of AI in Insights and Research 2024: Results and FindingsThe State of AI in Insights and Research 2024: Results and Findings
The State of AI in Insights and Research 2024: Results and Findings
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Production of Monoclonal Antibodies by Hybridoma Technology.pptx
Production of Monoclonal Antibodies by Hybridoma Technology.pptxProduction of Monoclonal Antibodies by Hybridoma Technology.pptx
Production of Monoclonal Antibodies by Hybridoma Technology.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
PRINCIPLE & APPLICATIONS OF IMMUNO BLOTTING TECHNIQUES.pptx
PRINCIPLE & APPLICATIONS OF IMMUNO BLOTTING TECHNIQUES.pptxPRINCIPLE & APPLICATIONS OF IMMUNO BLOTTING TECHNIQUES.pptx
PRINCIPLE & APPLICATIONS OF IMMUNO BLOTTING TECHNIQUES.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 

Constructor in java

  • 2. Topics for Today’s Session Constructor Difference between Method and Constructor Types of Constructors
  • 3. Constructors  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  In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created.  At the time of calling constructor, memory for the object is allocated in the memory.  Every time an object is created using the new() keyword, at least one constructor is called. Rules for creating Java constructor  Constructor name must be the same as its class name  A Constructor must have no explicit return type  A Java constructor cannot be abstract, static, final, and synchronized
  • 4. Types of Java constructors  There are two types of constructors in Java: 1. Default constructor (no-arg constructor) 2. Parameterized constructor Types of Constructors Default Constructor Parameterized Constructor
  • 5.  A constructor that have no parameter is known as default constructor.  Default constructor provides the default values to the object like 0, null etc. depending on the type.  If there is no constructor in a class, compiler automatically creates a default constructor. Syntax : <class_name>(){ }
  • 6. // Create a MyClass class class MyClass { int n; // Create a class attribute // Create a class constructor for the MyClass class public MyClass() { n = 10; // Set the initial value for the class attribute n } public static void main(String[] args) { MyClass myObj = new MyClass(); // Create an object of class MyClass // ( This will call the constructor) System.out.println(myObj.n); // Print the value of n } } // Outputs 10
  • 7. Example of default constructor that displays the default values class Student{ int id; String name; void display() { System.out.println(id+" "+name); } public static void main(String args[]){ Student s1=new Student(); Student s2=new Student(); s1.display(); s2.display(); } } Output 0 null 0 null Explanation: In the above class,you are not creating any constructor so compiler provides you a default constructor.Here 0 and null values are provided by default constructor.
  • 8. Parameterized constructor  Constructors that have parameters is known as parameterized constructor.  Parameterized constructor is used to provide different values to the distinct objects. Example of parameterized constructor /*In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor. */ class Student { int id; String name; Student (int i,String n){ id = i; name = n; } void display(){System.out.println(id+" "+name);} public static void main(String args[]) { Student s1 = new Student(111,"Karan"); Student s2 = new Student(222,"Aryan"); s1.display(); s2.display(); } }
  • 9. Difference between constructor and method in java There are many differences between constructors and methods. Constructor is used to initialize the state of an object. Method is used to expose behaviour of an object. Constructor must not have return type. Method must have return type. Constructor is invoked implicitly. Method is invoked explicitly. The java compiler provides a default constructor if you don't have any constructor. Method is not provided by compiler in any case. Constructor name must be same as the class name. Method name may or may not be same as class name.
  • 10. // Using Constructor class Car { int modYr; String modName; public Car(int y, String n) { modYr = y; modName = n; } public static void main(String[] args) { Car myCar = new Car(1969, "Mustang"); System.out.println(myCar.modYr + " " + myCar.modName); } } // Outputs 1969 Mustang // Using Mathods class Car { int modYr; String modName; void getdata(int y, String n) { modYr = y; modName = n; } void show() { System.out.println(modYr); System.out.println(modName); } public static void main(String[] args) { Car myCar = new Car( ) myCar.getdata(1969, "Mustang"); myCar.show(); } } // Outputs 1969 Mustang