SlideShare uma empresa Scribd logo
1 de 12
Initialization & Cleanup DhrubojyotiKayal
Guarantees initialization of every object Essentially a method Called when an object is created, even when no reference exists The 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 arguments public 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 method Exercise
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 distinguish Overloading on return value
Reference of the current object that is available to no static methods Used widely in constructors to avoid ambiguity Use if you want to pass a reference of current object to another object. this
There is no destructor in Java Memory is managed automatically by a garbage collector which is part of JVM A JVM may not even have a garbage collector When garbage collector runs is not under programmers control System.gc() – just a hint Common garbage collection technique Mark and Sweep finalization() Called before an object is garbage collected Useless method, should be avoided as you never know when it will be called Use in JNI programs – malloc() , free() Destruction
Q&A

Mais conteúdo relacionado

Mais procurados

Pointer to array and structure
Pointer to array and structurePointer to array and structure
Pointer to array and structuresangrampatil81
 
Structure in c language
Structure in c languageStructure in c language
Structure in c languagesangrampatil81
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructorrajshreemuthiah
 
Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143sanjeet kumar
 
When to use a structure vs classes in c++
When to use a structure vs classes in c++When to use a structure vs classes in c++
When to use a structure vs classes in c++Naman Kumar
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Abbott
 
Abstract data types
Abstract data typesAbstract data types
Abstract data typesHoang Nguyen
 
How to design an application correctly ?
How to design an application correctly ?How to design an application correctly ?
How to design an application correctly ?Guillaume AGIS
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and StructuresGem WeBlog
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structureshaibal sharif
 
Methods In C-Sharp (C#)
Methods In C-Sharp (C#)Methods In C-Sharp (C#)
Methods In C-Sharp (C#)Abid Kohistani
 

Mais procurados (20)

Generics collections
Generics collectionsGenerics collections
Generics collections
 
Pointer to array and structure
Pointer to array and structurePointer to array and structure
Pointer to array and structure
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Type casting
Type castingType casting
Type casting
 
Algo>Abstract data type
Algo>Abstract data typeAlgo>Abstract data type
Algo>Abstract data type
 
The Pain Points of C#
The Pain Points of C#The Pain Points of C#
The Pain Points of C#
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143Constructor & Destructor/sanjeet-1308143
Constructor & Destructor/sanjeet-1308143
 
Structure in c
Structure in cStructure in c
Structure in c
 
Structures
StructuresStructures
Structures
 
When to use a structure vs classes in c++
When to use a structure vs classes in c++When to use a structure vs classes in c++
When to use a structure vs classes in c++
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
How to design an application correctly ?
How to design an application correctly ?How to design an application correctly ?
How to design an application correctly ?
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Methods In C-Sharp (C#)
Methods In C-Sharp (C#)Methods In C-Sharp (C#)
Methods In C-Sharp (C#)
 
Data structures
Data structuresData structures
Data structures
 
Ppt lesson 08
Ppt lesson 08Ppt lesson 08
Ppt lesson 08
 

Destaque

Bsl Travel Pix
Bsl Travel PixBsl Travel Pix
Bsl Travel Pixsporck
 
Universal Yoga Profile
Universal Yoga ProfileUniversal Yoga Profile
Universal Yoga ProfileVijay Yoga
 
Going Mobile @ Balboa Park
Going Mobile @ Balboa ParkGoing Mobile @ Balboa Park
Going Mobile @ Balboa ParkTitus Bicknell
 
Helicopter Assessments - Improve your Customer Data Security!
Helicopter Assessments - Improve your Customer Data Security!Helicopter Assessments - Improve your Customer Data Security!
Helicopter Assessments - Improve your Customer Data Security!Dahamoo GmbH
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setupdhrubo kayal
 
Tak.To.Je.Ona
Tak.To.Je.OnaTak.To.Je.Ona
Tak.To.Je.Onavenom001
 
Seex feet under ibgles
Seex feet under ibglesSeex feet under ibgles
Seex feet under ibglesLidia Fonseca
 
Le Rocce Metamorfiche
Le Rocce MetamorficheLe Rocce Metamorfiche
Le Rocce Metamorfichematteo58
 
A Brief History of Conversation: Advertising in the Social Space
A Brief History of Conversation: Advertising in the Social SpaceA Brief History of Conversation: Advertising in the Social Space
A Brief History of Conversation: Advertising in the Social SpaceGordon Peters
 
Valve Interactive Capabilities 2008
Valve Interactive Capabilities 2008Valve Interactive Capabilities 2008
Valve Interactive Capabilities 2008dtjobrien
 
Sencha Complete: Kharkiv JS #1
Sencha Complete: Kharkiv JS #1Sencha Complete: Kharkiv JS #1
Sencha Complete: Kharkiv JS #1Illya Klymov
 
Hot Chocolate - Chocolate Caliente
Hot Chocolate  -  Chocolate CalienteHot Chocolate  -  Chocolate Caliente
Hot Chocolate - Chocolate CalienteEduardo Cortes
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lispkyleburton
 
Fuzzy String Matching
Fuzzy String MatchingFuzzy String Matching
Fuzzy String Matchingkyleburton
 

Destaque (19)

Bsl Travel Pix
Bsl Travel PixBsl Travel Pix
Bsl Travel Pix
 
Universal Yoga Profile
Universal Yoga ProfileUniversal Yoga Profile
Universal Yoga Profile
 
Going Mobile @ Balboa Park
Going Mobile @ Balboa ParkGoing Mobile @ Balboa Park
Going Mobile @ Balboa Park
 
Helicopter Assessments - Improve your Customer Data Security!
Helicopter Assessments - Improve your Customer Data Security!Helicopter Assessments - Improve your Customer Data Security!
Helicopter Assessments - Improve your Customer Data Security!
 
12 encapsulation
12   encapsulation12   encapsulation
12 encapsulation
 
Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
19 reflection
19   reflection19   reflection
19 reflection
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
Tak.To.Je.Ona
Tak.To.Je.OnaTak.To.Je.Ona
Tak.To.Je.Ona
 
Asd
AsdAsd
Asd
 
Seex feet under ibgles
Seex feet under ibglesSeex feet under ibgles
Seex feet under ibgles
 
Le Rocce Metamorfiche
Le Rocce MetamorficheLe Rocce Metamorfiche
Le Rocce Metamorfiche
 
A Brief History of Conversation: Advertising in the Social Space
A Brief History of Conversation: Advertising in the Social SpaceA Brief History of Conversation: Advertising in the Social Space
A Brief History of Conversation: Advertising in the Social Space
 
Valve Interactive Capabilities 2008
Valve Interactive Capabilities 2008Valve Interactive Capabilities 2008
Valve Interactive Capabilities 2008
 
Sencha Complete: Kharkiv JS #1
Sencha Complete: Kharkiv JS #1Sencha Complete: Kharkiv JS #1
Sencha Complete: Kharkiv JS #1
 
Hot Chocolate - Chocolate Caliente
Hot Chocolate  -  Chocolate CalienteHot Chocolate  -  Chocolate Caliente
Hot Chocolate - Chocolate Caliente
 
Intro To Git
Intro To GitIntro To Git
Intro To Git
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
 
Fuzzy String Matching
Fuzzy String MatchingFuzzy String Matching
Fuzzy String Matching
 

Semelhante a 14 initialization & cleanup

Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywordsmanish kumar
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Andrew Petryk
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Javakjkleindorfer
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0BG Java EE Course
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined MethodPRN USM
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Featuresindia_mani
 

Semelhante a 14 initialization & cleanup (20)

OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
java tutorial 3
 java tutorial 3 java tutorial 3
java tutorial 3
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
 
Java generics
Java genericsJava generics
Java generics
 
Oop lecture5
Oop lecture5Oop lecture5
Oop lecture5
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
Defining classes-and-objects-1.0
Defining classes-and-objects-1.0Defining classes-and-objects-1.0
Defining classes-and-objects-1.0
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Linq Introduction
Linq IntroductionLinq Introduction
Linq Introduction
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Java execise
Java execiseJava execise
Java execise
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Features
 
Polymorphism.pptx
Polymorphism.pptxPolymorphism.pptx
Polymorphism.pptx
 
Creating Interface- Practice Program 6.docx
Creating Interface- Practice Program 6.docxCreating Interface- Practice Program 6.docx
Creating Interface- Practice Program 6.docx
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Java2
Java2Java2
Java2
 

Mais de dhrubo kayal

Mais de dhrubo kayal (17)

01 session tracking
01   session tracking01   session tracking
01 session tracking
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
 
18 concurrency
18   concurrency18   concurrency
18 concurrency
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
16 containers
16   containers16   containers
16 containers
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
11 static
11   static11   static
11 static
 
10 access control
10   access control10   access control
10 access control
 
09 packages
09   packages09   packages
09 packages
 
07 flow control
07   flow control07   flow control
07 flow control
 
05 operators
05   operators05   operators
05 operators
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 
02 what is java
02   what is java02   what is java
02 what is java
 
01 handshake
01   handshake01   handshake
01 handshake
 

14 initialization & cleanup

  • 1. Initialization & Cleanup DhrubojyotiKayal
  • 2. Guarantees initialization of every object Essentially a method Called when an object is created, even when no reference exists The 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 arguments public 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 method Exercise
  • 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 distinguish Overloading on return value
  • 10. Reference of the current object that is available to no static methods Used widely in constructors to avoid ambiguity Use if you want to pass a reference of current object to another object. this
  • 11. There is no destructor in Java Memory is managed automatically by a garbage collector which is part of JVM A JVM may not even have a garbage collector When garbage collector runs is not under programmers control System.gc() – just a hint Common garbage collection technique Mark and Sweep finalization() Called before an object is garbage collected Useless method, should be avoided as you never know when it will be called Use in JNI programs – malloc() , free() Destruction
  • 12. Q&A