SlideShare a Scribd company logo
1 of 36
CONSTRUCTORS
• 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.
• 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.
• It calls a default constructor if there is no
constructor available in the class. In such case,
Java compiler provides a default constructor
by default.
• There are two types of constructors in Java:
– no-arg constructor, and parameterized constructor.
• Note: It is called constructor because it
constructs the values at the time of object
creation.
• It is not necessary to write a constructor for a
class.
• It is because java compiler creates a default
constructor if your class doesn't have any.
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
• Note: We can use access modifiers while
declaring a constructor. It controls the object
creation.
• In other words, we can have private,
protected, public or default constructor in
Java.
• There are two types of constructors in Java:
– Default constructor (no-arg constructor)
– Parameterized constructor
Java Default Constructor
• A constructor is called "Default Constructor"
when it doesn't have any parameter.
Example of default constructor
• Rule: If there is no constructor in a class,
compiler automatically creates a default
constructor.
What is the purpose of a default
constructor?
• The default constructor is used to provide the
default values to the object like 0, null, etc.,
depending on the type.
• 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.
Java Parameterized Constructor
• A constructor which has a specific number of
parameters is called a parameterized
constructor.
• Why use the parameterized constructor?
• The parameterized constructor is used to
provide different values to distinct objects.
However, you can provide the same values
also.
• 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.
Constructor Overloading in Java
• In Java, a constructor is just like a method but
without return type. It can also be overloaded
like Java methods.
• Constructor overloading in Java is a technique of
having more than one constructor with different
parameter lists.
• They are arranged in a way that each constructor
performs a different task.
• They are differentiated by the compiler by the
number of parameters in the list and their types.
Copying values without constructor
• We can copy the values of one object into
another by assigning the objects values to
another object. In this case, there is no need
to create the constructor.
Does constructor return any value?
• yes, it is the current class instance (You cannot
use return type yet it returns a value).
Can constructor perform other tasks
instead of initialization?
• Yes, like object creation, starting a thread,
calling a method, etc. You can perform any
operation in the constructor as you perform in
the method.
Is there Constructor class in Java?
• Yes.
Java static keyword
• The static keyword in Java is used for memory
management mainly. We can apply static
keyword with variables, methods, blocks
and nested classes. The static keyword belongs to
the class than an instance of the class.
• The static can be:
– Variable (also known as a class variable)
– Method (also known as a class method)
– Block
– Nested class
Java static variable
• If you declare any variable as static, it is known as a
static variable.
• The static variable can be used to refer to the common
property of all objects (which is not unique for each
object), for example, the company name of employees,
college name of students, etc.
• The static variable gets memory only once in the class
area at the time of class loading.
• Advantages of static variable
– It makes your program memory efficient (i.e., it saves
memory).
• Suppose there are 500 students in my college,
now all instance data members will get
memory each time when the object is created.
All students have its unique rollno and name,
so instance data member is good in such case.
Here, "college" refers to the common property
of all objects. If we make it static, this field will
get the memory only once.
– Java static property is shared to all objects.
CONSTRUCTORS.pptx
CONSTRUCTORS.pptx

More Related Content

Similar to CONSTRUCTORS.pptx

Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOPMuhammad Hammad Waseem
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptxSrizan Pokrel
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
OOP static variables - friend function - operator overloading - copy constructor
OOP static variables - friend function - operator overloading - copy constructorOOP static variables - friend function - operator overloading - copy constructor
OOP static variables - friend function - operator overloading - copy constructorTamir Azrab
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardHari kiran G
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxethiouniverse
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
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
 
2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other toolskinan keshkeh
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingmsneha
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in javaagorolabs
 
Lec08 constructors
Lec08   constructorsLec08   constructors
Lec08 constructorsAsif Shahzad
 

Similar to CONSTRUCTORS.pptx (20)

Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Enumerations in java.pptx
Enumerations in java.pptxEnumerations in java.pptx
Enumerations in java.pptx
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
OOP static variables - friend function - operator overloading - copy constructor
OOP static variables - friend function - operator overloading - copy constructorOOP static variables - friend function - operator overloading - copy constructor
OOP static variables - friend function - operator overloading - copy constructor
 
Java
JavaJava
Java
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference CardOCP Java (OCPJP) 8 Exam Quick Reference Card
OCP Java (OCPJP) 8 Exam Quick Reference Card
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
Module 1.pptx
Module 1.pptxModule 1.pptx
Module 1.pptx
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
 
Java
JavaJava
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
 
C# structure
C# structureC# structure
C# structure
 
2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
OOP in JS
OOP in JSOOP in JS
OOP in JS
 
Java 102 intro to object-oriented programming in java
Java 102   intro to object-oriented programming in javaJava 102   intro to object-oriented programming in java
Java 102 intro to object-oriented programming in java
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
Lec08 constructors
Lec08   constructorsLec08   constructors
Lec08 constructors
 

Recently uploaded

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 

Recently uploaded (20)

Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 

CONSTRUCTORS.pptx

  • 2. • 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. • It is a special type of method which is used to initialize the object.
  • 3. • Every time an object is created using the new() keyword, at least one constructor is called. • It calls a default constructor if there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.
  • 4. • There are two types of constructors in Java: – no-arg constructor, and parameterized constructor. • Note: It is called constructor because it constructs the values at the time of object creation. • It is not necessary to write a constructor for a class. • It is because java compiler creates a default constructor if your class doesn't have any.
  • 5. 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
  • 6. • Note: We can use access modifiers while declaring a constructor. It controls the object creation. • In other words, we can have private, protected, public or default constructor in Java.
  • 7. • There are two types of constructors in Java: – Default constructor (no-arg constructor) – Parameterized constructor
  • 8. Java Default Constructor • A constructor is called "Default Constructor" when it doesn't have any parameter.
  • 9. Example of default constructor
  • 10. • Rule: If there is no constructor in a class, compiler automatically creates a default constructor.
  • 11. What is the purpose of a default constructor? • The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.
  • 12.
  • 13. • 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.
  • 14. Java Parameterized Constructor • A constructor which has a specific number of parameters is called a parameterized constructor.
  • 15. • Why use the parameterized constructor? • The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.
  • 16. • 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.
  • 17.
  • 18.
  • 19. Constructor Overloading in Java • In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods. • Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. • They are arranged in a way that each constructor performs a different task. • They are differentiated by the compiler by the number of parameters in the list and their types.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24. Copying values without constructor • We can copy the values of one object into another by assigning the objects values to another object. In this case, there is no need to create the constructor.
  • 25.
  • 26.
  • 27. Does constructor return any value? • yes, it is the current class instance (You cannot use return type yet it returns a value).
  • 28. Can constructor perform other tasks instead of initialization? • Yes, like object creation, starting a thread, calling a method, etc. You can perform any operation in the constructor as you perform in the method.
  • 29. Is there Constructor class in Java? • Yes.
  • 30. Java static keyword • The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class. • The static can be: – Variable (also known as a class variable) – Method (also known as a class method) – Block – Nested class
  • 31.
  • 32. Java static variable • If you declare any variable as static, it is known as a static variable. • The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. • The static variable gets memory only once in the class area at the time of class loading. • Advantages of static variable – It makes your program memory efficient (i.e., it saves memory).
  • 33.
  • 34. • Suppose there are 500 students in my college, now all instance data members will get memory each time when the object is created. All students have its unique rollno and name, so instance data member is good in such case. Here, "college" refers to the common property of all objects. If we make it static, this field will get the memory only once. – Java static property is shared to all objects.