SlideShare a Scribd company logo
Initialization & CleanupDhrubojyotiKayal
Guarantees initialization of every objectEssentially a methodCalled when an object is created, even when no reference existsThe name of the constructor is the same as the name of the class Unusual type of method because it has no return value the new expression does return a reference to the newly created object public class Shape {	public Shape() {}}Shape s = new Shape()Constructor
A constructor that takes no arguments is called the default constructor If you provide no constructor the default constructor is provided by default by the compiler.You can definitely have constructors with one or more argumentspublic class Rectangle{	private int length;	private int breadth;	public Rectangle(int length, int breadth) {		length = length;		breadth = breadth;	}}Default Constructor
Method overloading allow the same method name to be used with different argument types. Constructors are methods and they can be overloaded too.Method overloading
public class Tree { int height; 	public Tree() { 		print("Planting a seedling"); 		height = 0; 	} 	public Tree(intinitialHeight) { 		height = initialHeight; 		print("Creating new Tree that is " + 		height + " feet tall"); 	}	public void info() { 		print("Tree is " + height + " feet tall"); 	} 	public void info(String s) { 		print(s + ": Tree is " + height + " feet tall"); 	}  }Overloading in Action
Test the Tree class by invoking each constructor and each methodExercise
Each overloaded method must take a unique list of argument types Even differences in the ordering of arguments are sufficient to distinguish two methods, although you don’t normally want to take this approach because it produces difficult-to-maintain code Distinguishing overloaded method
public class OverloadingOrder { 	static void f(String s, inti) { 		print("String: " + s + ", int: " + i); 	} 	static void f(inti, String s) { 		print("int: " + i + ", String: " + s); 	} 	public static void main(String[] args) { 		f("String first", 11); 		f(99, "Int first"); 	} } Overloading in Action
void f() {} int f() { return 1; }int x = f();f();You cannot use return value types to distinguishOverloading on return value
Reference of the current object that is available to no static methodsUsed widely in constructors to avoid ambiguityUse if you want to pass a reference of current object to another object.this
There is no destructor in JavaMemory is managed automatically by a garbage collector which is part of JVMA JVM may not even have a garbage collectorWhen garbage collector runs is not under programmers controlSystem.gc() – just a hintCommon garbage collection techniqueMark and Sweepfinalization()Called before an object is garbage collectedUseless method, should be avoided as you never know when it will be calledUse in JNI programs – malloc() , free()Destruction
Q&A

More Related Content

PDF
Algorithms: I
PPTX
08 class and object
PPTX
Type casting in java
PPTX
PPTX
Primitives in Generics
PPTX
PPT
Structure in C
PPTX
Structures in c language
Algorithms: I
08 class and object
Type casting in java
Primitives in Generics
Structure in C
Structures in c language

What's hot (20)

PPT
Generics collections
PPTX
Pointer to array and structure
PPTX
Structure in c language
PPTX
Type casting
PPT
Algo>Abstract data type
PPTX
The Pain Points of C#
PPTX
Constructor and destructor
PPTX
Constructor & Destructor/sanjeet-1308143
PPT
Structure in c
PPT
Structures
PPTX
When to use a structure vs classes in c++
DOC
Data structure lecture 2
PPT
Abstract data types
PPTX
How to design an application correctly ?
PDF
Pointers and Structures
PPTX
Array Of Pointers
PPTX
C programing -Structure
PPTX
Methods In C-Sharp (C#)
PPTX
Data structures
PPT
Ppt lesson 08
Generics collections
Pointer to array and structure
Structure in c language
Type casting
Algo>Abstract data type
The Pain Points of C#
Constructor and destructor
Constructor & Destructor/sanjeet-1308143
Structure in c
Structures
When to use a structure vs classes in c++
Data structure lecture 2
Abstract data types
How to design an application correctly ?
Pointers and Structures
Array Of Pointers
C programing -Structure
Methods In C-Sharp (C#)
Data structures
Ppt lesson 08
Ad

Viewers also liked (19)

PPT
Bsl Travel Pix
PDF
Universal Yoga Profile
ZIP
Going Mobile @ Balboa Park
PDF
Helicopter Assessments - Improve your Customer Data Security!
PPTX
12 encapsulation
XLSX
Cipla 20-09-2010
PPTX
19 reflection
PPTX
01 overview-servlets-and-environment-setup
PPT
Tak.To.Je.Ona
PPT
PPT
Seex feet under ibgles
PPT
Le Rocce Metamorfiche
PPT
A Brief History of Conversation: Advertising in the Social Space
PDF
Valve Interactive Capabilities 2008
PPT
Sencha Complete: Kharkiv JS #1
PPS
Hot Chocolate - Chocolate Caliente
PDF
Intro To Git
PDF
Introduction To Lisp
PDF
Fuzzy String Matching
Bsl Travel Pix
Universal Yoga Profile
Going Mobile @ Balboa Park
Helicopter Assessments - Improve your Customer Data Security!
12 encapsulation
Cipla 20-09-2010
19 reflection
01 overview-servlets-and-environment-setup
Tak.To.Je.Ona
Seex feet under ibgles
Le Rocce Metamorfiche
A Brief History of Conversation: Advertising in the Social Space
Valve Interactive Capabilities 2008
Sencha Complete: Kharkiv JS #1
Hot Chocolate - Chocolate Caliente
Intro To Git
Introduction To Lisp
Fuzzy String Matching
Ad

Similar to 14 initialization & cleanup (20)

PDF
OOPs & Inheritance Notes
PPT
java tutorial 3
PPTX
Chapter 6.6
PPTX
Lecture 4_Java Method-constructor_imp_keywords
PPTX
Java generics
PPT
Oop lecture5
PPT
Generic Types in Java (for ArtClub @ArtBrains Software)
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
PPT
Java Generics
PPTX
Week9 Intro to classes and objects in Java
PPT
Defining classes-and-objects-1.0
PPT
Class & Object - User Defined Method
PPT
Java tutorial for Beginners and Entry Level
PPTX
Linq Introduction
PDF
Object Oriented Solved Practice Programs C++ Exams
PPT
Object Oriented Programming Concept.Hello
DOCX
Java execise
PPTX
Generic Collections and learn how to use it
PPT
Jdk1.5 Features
PPTX
Polymorphism.pptx
OOPs & Inheritance Notes
java tutorial 3
Chapter 6.6
Lecture 4_Java Method-constructor_imp_keywords
Java generics
Oop lecture5
Generic Types in Java (for ArtClub @ArtBrains Software)
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
Java Generics
Week9 Intro to classes and objects in Java
Defining classes-and-objects-1.0
Class & Object - User Defined Method
Java tutorial for Beginners and Entry Level
Linq Introduction
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Programming Concept.Hello
Java execise
Generic Collections and learn how to use it
Jdk1.5 Features
Polymorphism.pptx

More from dhrubo kayal (17)

PPTX
01 session tracking
PPTX
03 handling requests
PPTX
02 up close with servlets
PPTX
18 concurrency
PPTX
17 exceptions
PPTX
16 containers
PPTX
15 interfaces
PPTX
13 inheritance
PPTX
11 static
PPTX
10 access control
PPTX
09 packages
PPTX
07 flow control
PPTX
05 operators
PPTX
04 data types & variables
PPTX
03 hello world with java
PPTX
02 what is java
PPTX
01 handshake
01 session tracking
03 handling requests
02 up close with servlets
18 concurrency
17 exceptions
16 containers
15 interfaces
13 inheritance
11 static
10 access control
09 packages
07 flow control
05 operators
04 data types & variables
03 hello world with java
02 what is java
01 handshake

14 initialization & cleanup

  • 2. Guarantees initialization of every objectEssentially a methodCalled when an object is created, even when no reference existsThe name of the constructor is the same as the name of the class Unusual type of method because it has no return value the new expression does return a reference to the newly created object public class Shape { public Shape() {}}Shape s = new Shape()Constructor
  • 3. A constructor that takes no arguments is called the default constructor If you provide no constructor the default constructor is provided by default by the compiler.You can definitely have constructors with one or more argumentspublic class Rectangle{ private int length; private int breadth; public Rectangle(int length, int breadth) { length = length; breadth = breadth; }}Default Constructor
  • 4. Method overloading allow the same method name to be used with different argument types. Constructors are methods and they can be overloaded too.Method overloading
  • 5. public class Tree { int height; public Tree() { print("Planting a seedling"); height = 0; } public Tree(intinitialHeight) { height = initialHeight; print("Creating new Tree that is " + height + " feet tall"); } public void info() { print("Tree is " + height + " feet tall"); } public void info(String s) { print(s + ": Tree is " + height + " feet tall"); } }Overloading in Action
  • 6. Test the Tree class by invoking each constructor and each methodExercise
  • 7. Each overloaded method must take a unique list of argument types Even differences in the ordering of arguments are sufficient to distinguish two methods, although you don’t normally want to take this approach because it produces difficult-to-maintain code Distinguishing overloaded method
  • 8. public class OverloadingOrder { static void f(String s, inti) { print("String: " + s + ", int: " + i); } static void f(inti, String s) { print("int: " + i + ", String: " + s); } public static void main(String[] args) { f("String first", 11); f(99, "Int first"); } } Overloading in Action
  • 9. void f() {} int f() { return 1; }int x = f();f();You cannot use return value types to distinguishOverloading on return value
  • 10. Reference of the current object that is available to no static methodsUsed widely in constructors to avoid ambiguityUse if you want to pass a reference of current object to another object.this
  • 11. There is no destructor in JavaMemory is managed automatically by a garbage collector which is part of JVMA JVM may not even have a garbage collectorWhen garbage collector runs is not under programmers controlSystem.gc() – just a hintCommon garbage collection techniqueMark and Sweepfinalization()Called before an object is garbage collectedUseless method, should be avoided as you never know when it will be calledUse in JNI programs – malloc() , free()Destruction
  • 12. Q&A