SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
Core Java

Debasish Pratihari

Method Declaration:



Method declaration has two parts
 Method Header
 Method Body
Method Header :
o consists of modifiers (optional), return
type, method name, parameter list and a
throws clause (optional)
o types of modifiers





private
public
protected
final






Abstract
Static
synchronized
native

Note :

 Static methods can
only access static
methods and static
fields.
 Final methods
cannot be overridden in sub-class
 Abstract methods
have no body

Method invocations :


invoked as operations on objects/classes using
the dot ( . ) operator



static method:
o Outside of the class: “reference” can
either be the class name or an object
reference belonging to the class
o Inside the class: “reference” can be
omitted



non-static method:
o “reference” must be an object reference

Method overloading:


A class can have more than one method with
the same name as long as they have different
parameter list.
public class Lakshya {
...
public void setPrice (float newPrice) {
price = newPrice;
}
public void setPrice () {
price = 0;
}
}

1Lecture/core/oops2/09
Page #1

feel the Technology…
Core Java

Debasish Pratihari

main method :


the system locates and runs the main method
for a class when you run a program



other methods get execution when called by the
main method explicitly or implicitly



must be public, static and void

Constructor:













is special member method
has the same name of its class
doesn’t return any thing (even if void)
can only be called with new operator to create
object
There must be at least one constructor for every
class
If no explicit constructor is there in class then
java language provides one called default
constructor

The access specifier of the default constructor is
same as the class.
is never inherited
can be parameterized and overloaded
can be used to initialized the object
if there is only parameterized constructors
present in the super class , then it is became
mandatory to provide a constructor in the sub
class.

2Lecture/core/oops2/09
Page #2

feel the Technology…
Core Java

Debasish Pratihari

Default Constructor :




Java provides a default constructor which takes
no arguments and performs no special actions
or initializations, when no explicit constructors
are provided.
The only action taken by the implicit default
constructor is to call the super class constructor
using the super() call.

class Lakshya{
Lakshya(){
super();
}

}

Default
Constructor

public class Lakshya{
public Lakshya(){
super();
}

}

Java overloaded constructor :
Like java method java constructor can be over loaded.
class Lakshya{
Lakshya(){
……
}

Constructor 1

Lakshya (String name){
……
}
}

Note :
 If a class is having
more than one
constructor it gives
the flexibility to the
programmer to
instantiate the class
in different fashion.

Constructor 2

Constructor Chaining :


Every constructor calls its super class constructor. An
implied super() is therefore included in each
constructor which does not include either the this()
function or an explicit super() call as its first
statement. The super() statement invokes a
constructor of the super class.



The implicit super() can be replaced by an explicit
super(). The super statement must be the first
statement of the constructor.



It is possible to use this() construct, to implement
local chaining of constructors in a class. The this() call
in a constructor invokes the an other constructor with
the corresponding parameter list within the same
class.

3Lecture/core/oops2/09
Page #3

feel the Technology…
Core Java

Debasish Pratihari

Example :
public class Lakshya{
Lakshya(){
System.out.println("Hello");
}
Lakshya(int i){
this();
System.out.println("i="+i);
}
}

Arbitrary Number of Arguments :
25% 

Note :
 Parameters refers to
the list of variables
in a method
declaration.
Arguments are the
actual values that
are passed.

a construct called varargs to pass an arbitrary
number of values to a method

Example:
public class Lakshya{
Lakshya(int...i){
for(int c=0; c<i.length;c++)
System.out.println(i[c]);
}
public static void main(String args[]){
int nums[]={1,2,3,4,5};
Lakshya ob1= new Lakshya(10,20);
Lakshya ob2= new Lakshya(10,20,30,40);
Lakshya ob3= new Lakshya(nums);

 Varargs are treated
same as arrays.

}
}
Accessing shadowed Arguments :
 A parameter can have the same name as one of
the class's fields. If this is the case, the
parameter is said to shadow the field.
 “this” keyword is used to access the shadowed
fields :
public class Lakshya{
int a,b
Lakshya(int a, int b){
this.a=a;
this.b=b;
}
}

Note :
 Within an instance
method or a
constructor, this is a
reference to the
current object. It is
not available to
static methods.

4Lecture/core/oops2/09
Page #4

feel the Technology…
Core Java

Debasish Pratihari

Initializing Fields :


Simple initialization can be done inline.
class Lakshya {
int a=10,b=20;
….
}



If initialization involves some kind of logic then
constructors are useful.



The facility given by constructors for instance
variables can be achieved through static
initializers for class variables.

Static Initializers:
 A static initialization block is a normal block of
25%
code enclosed with curly braces and preceded
by the static keyword.
class Lakshya{
….
satic{
// initialization code here
}
}



A class can have multiple static block.



Static blocks are executed exactly in the order
as they present in the source code.

Initializer Blocks :





Java provides an initializer block as an
alternative to constructor .
Initializer blocks for instance variable are like
static initializer blocks, but without the static
keyword.
The initializer block is copied into all the
constructors without a call to this().
The block can be useful to avoid code
redundancy from constructors.

5Lecture/core/oops2/09
Page #5

feel the Technology…
Core Java

Debasish Pratihari

Java Objects:






Java object can be created by using the new
keyword.
They are stored in JVM’s heap area.
Java objects are nameless.
They can be accessed through their reference
only.
They are garbage collected if they don’t have
any reference.

Object & Object Reference
25%

Lakshya ob = new Lakshya( );

Note:

Heap Area

 There is another
way to create
 objects using

100
ob

newInstance()
method of Class
“Class”.

Object reference
100

(Object of
Lakshya class)

Garbage Collection :






It the process of automatically destroying objects
that are no-longer referenced by the program.
It is a process for automatic memory
management.
Basic principles of garbage Collection:
o Locate objects that can’t be accessed in
future.
o Re-claim the resources used by those
objects.
In java an object may have a finalizer: a method
that the garbage collector must run on the object
prior to freeing the object.

Note :
 The Finalizer
method in java in
finalize().


Finalizers are
useful to avoid
Resource
Leak.

6Lecture/core/oops2/09
Page #6

feel the Technology…

Mais conteúdo relacionado

Mais procurados

Java Serialization Deep Dive
Java Serialization Deep DiveJava Serialization Deep Dive
Java Serialization Deep DiveMartijn Dashorst
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List Hitesh-Java
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)SURBHI SAROHA
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in javaAtul Sehdev
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in pythonSantosh Verma
 
Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NETAbhi Arya
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Java Collections Framework
Java  Collections  FrameworkJava  Collections  Framework
Java Collections Frameworkguestd8c458
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Hitesh-Java
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanZeeshan Khan
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsFrançois Garillot
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysDevaKumari Vijay
 

Mais procurados (20)

Java collections notes
Java collections notesJava collections notes
Java collections notes
 
Java Serialization Deep Dive
Java Serialization Deep DiveJava Serialization Deep Dive
Java Serialization Deep Dive
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 
Java Unit 2(Part 1)
Java Unit 2(Part 1)Java Unit 2(Part 1)
Java Unit 2(Part 1)
 
Java Unit 2 (Part 2)
Java Unit 2 (Part 2)Java Unit 2 (Part 2)
Java Unit 2 (Part 2)
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
 
Java Unit 2(part 3)
Java Unit 2(part 3)Java Unit 2(part 3)
Java Unit 2(part 3)
 
Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NET
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Java Collections Framework
Java  Collections  FrameworkJava  Collections  Framework
Java Collections Framework
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshan
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on Steroids
 
Object Class
Object Class Object Class
Object Class
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 

Destaque

Collaboration for successful e-learning in Worcestershire
Collaboration for successful e-learning in WorcestershireCollaboration for successful e-learning in Worcestershire
Collaboration for successful e-learning in WorcestershirePaul McElvaney
 
Learning Pool: Work life balance 1 the secrets to building confidence resili...
Learning Pool: Work life balance 1  the secrets to building confidence resili...Learning Pool: Work life balance 1  the secrets to building confidence resili...
Learning Pool: Work life balance 1 the secrets to building confidence resili...Paul McElvaney
 
香港六合彩
香港六合彩香港六合彩
香港六合彩wejia
 
Fip lezing Istanbul deel 3
Fip lezing Istanbul deel 3Fip lezing Istanbul deel 3
Fip lezing Istanbul deel 3Sjef Kerkhofs
 
Learning Pool Social Care Seminar
Learning Pool Social Care SeminarLearning Pool Social Care Seminar
Learning Pool Social Care SeminarPaul McElvaney
 
Click frog present2013-agecy2
Click frog present2013-agecy2Click frog present2013-agecy2
Click frog present2013-agecy2Ivan Zhyvayev
 
Editing: It's not as easy as it looks
Editing: It's not as easy as it looksEditing: It's not as easy as it looks
Editing: It's not as easy as it looksRhonda Bracey
 
18.05.08 Acaosocial
18.05.08   Acaosocial18.05.08   Acaosocial
18.05.08 AcaosocialJubrac Jacui
 
Niver Luciano - 24.02.07
Niver Luciano - 24.02.07Niver Luciano - 24.02.07
Niver Luciano - 24.02.07Jubrac Jacui
 
Zolimome_Press release
Zolimome_Press releaseZolimome_Press release
Zolimome_Press releasezolimome
 
¡Basta de escuchar a los usuarios!
¡Basta de escuchar a los usuarios! ¡Basta de escuchar a los usuarios!
¡Basta de escuchar a los usuarios! Pedro Arellano
 
Workshop Colin 2 Feb 2009
Workshop Colin 2 Feb 2009Workshop Colin 2 Feb 2009
Workshop Colin 2 Feb 2009Sjef Kerkhofs
 
Reviewing Screen-Based Content
Reviewing Screen-Based ContentReviewing Screen-Based Content
Reviewing Screen-Based ContentRhonda Bracey
 

Destaque (20)

Social mediacongres
Social mediacongresSocial mediacongres
Social mediacongres
 
Collaboration for successful e-learning in Worcestershire
Collaboration for successful e-learning in WorcestershireCollaboration for successful e-learning in Worcestershire
Collaboration for successful e-learning in Worcestershire
 
Learning Pool: Work life balance 1 the secrets to building confidence resili...
Learning Pool: Work life balance 1  the secrets to building confidence resili...Learning Pool: Work life balance 1  the secrets to building confidence resili...
Learning Pool: Work life balance 1 the secrets to building confidence resili...
 
Guia de blogger
Guia de bloggerGuia de blogger
Guia de blogger
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Wheredo Ibegin
Wheredo IbeginWheredo Ibegin
Wheredo Ibegin
 
Fip lezing Istanbul deel 3
Fip lezing Istanbul deel 3Fip lezing Istanbul deel 3
Fip lezing Istanbul deel 3
 
Learning Pool Social Care Seminar
Learning Pool Social Care SeminarLearning Pool Social Care Seminar
Learning Pool Social Care Seminar
 
Click frog present2013-agecy2
Click frog present2013-agecy2Click frog present2013-agecy2
Click frog present2013-agecy2
 
Editing: It's not as easy as it looks
Editing: It's not as easy as it looksEditing: It's not as easy as it looks
Editing: It's not as easy as it looks
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
18.05.08 Acaosocial
18.05.08   Acaosocial18.05.08   Acaosocial
18.05.08 Acaosocial
 
Niver Luciano - 24.02.07
Niver Luciano - 24.02.07Niver Luciano - 24.02.07
Niver Luciano - 24.02.07
 
Zolimome_Press release
Zolimome_Press releaseZolimome_Press release
Zolimome_Press release
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
110117 markets
110117 markets110117 markets
110117 markets
 
¡Basta de escuchar a los usuarios!
¡Basta de escuchar a los usuarios! ¡Basta de escuchar a los usuarios!
¡Basta de escuchar a los usuarios!
 
Workshop Colin 2 Feb 2009
Workshop Colin 2 Feb 2009Workshop Colin 2 Feb 2009
Workshop Colin 2 Feb 2009
 
Reviewing Screen-Based Content
Reviewing Screen-Based ContentReviewing Screen-Based Content
Reviewing Screen-Based Content
 

Semelhante a Lecture 9

UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptxakila m
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxchetanpatilcp783
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMambikavenkatesh2
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)Khaled Anaqwa
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#foreverredpb
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfismartshanker1
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.Tarunsingh198
 
Java assignment help
Java assignment helpJava assignment help
Java assignment helpJacob William
 
Java tutorials
Java tutorialsJava tutorials
Java tutorialssaryu2011
 

Semelhante a Lecture 9 (20)

C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Java basics
Java basicsJava basics
Java basics
 
Java Reflection Concept and Working
Java Reflection Concept and WorkingJava Reflection Concept and Working
Java Reflection Concept and Working
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
Lecture22.23.07.2014
Lecture22.23.07.2014Lecture22.23.07.2014
Lecture22.23.07.2014
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
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
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Java assignment help
Java assignment helpJava assignment help
Java assignment help
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Java mcq
Java mcqJava mcq
Java mcq
 

Mais de Debasish Pratihari (16)

Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Lecture 20
Lecture 20Lecture 20
Lecture 20
 
Lecture 19
Lecture 19Lecture 19
Lecture 19
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Lecture 16
Lecture 16Lecture 16
Lecture 16
 
Lecture 14
Lecture 14Lecture 14
Lecture 14
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture25
Lecture25Lecture25
Lecture25
 

Último

Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 

Último (20)

Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 

Lecture 9

  • 1. Core Java Debasish Pratihari Method Declaration:   Method declaration has two parts  Method Header  Method Body Method Header : o consists of modifiers (optional), return type, method name, parameter list and a throws clause (optional) o types of modifiers     private public protected final     Abstract Static synchronized native Note :  Static methods can only access static methods and static fields.  Final methods cannot be overridden in sub-class  Abstract methods have no body Method invocations :  invoked as operations on objects/classes using the dot ( . ) operator  static method: o Outside of the class: “reference” can either be the class name or an object reference belonging to the class o Inside the class: “reference” can be omitted  non-static method: o “reference” must be an object reference Method overloading:  A class can have more than one method with the same name as long as they have different parameter list. public class Lakshya { ... public void setPrice (float newPrice) { price = newPrice; } public void setPrice () { price = 0; } } 1Lecture/core/oops2/09 Page #1 feel the Technology…
  • 2. Core Java Debasish Pratihari main method :  the system locates and runs the main method for a class when you run a program  other methods get execution when called by the main method explicitly or implicitly  must be public, static and void Constructor:            is special member method has the same name of its class doesn’t return any thing (even if void) can only be called with new operator to create object There must be at least one constructor for every class If no explicit constructor is there in class then java language provides one called default constructor The access specifier of the default constructor is same as the class. is never inherited can be parameterized and overloaded can be used to initialized the object if there is only parameterized constructors present in the super class , then it is became mandatory to provide a constructor in the sub class. 2Lecture/core/oops2/09 Page #2 feel the Technology…
  • 3. Core Java Debasish Pratihari Default Constructor :   Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided. The only action taken by the implicit default constructor is to call the super class constructor using the super() call. class Lakshya{ Lakshya(){ super(); } } Default Constructor public class Lakshya{ public Lakshya(){ super(); } } Java overloaded constructor : Like java method java constructor can be over loaded. class Lakshya{ Lakshya(){ …… } Constructor 1 Lakshya (String name){ …… } } Note :  If a class is having more than one constructor it gives the flexibility to the programmer to instantiate the class in different fashion. Constructor 2 Constructor Chaining :  Every constructor calls its super class constructor. An implied super() is therefore included in each constructor which does not include either the this() function or an explicit super() call as its first statement. The super() statement invokes a constructor of the super class.  The implicit super() can be replaced by an explicit super(). The super statement must be the first statement of the constructor.  It is possible to use this() construct, to implement local chaining of constructors in a class. The this() call in a constructor invokes the an other constructor with the corresponding parameter list within the same class. 3Lecture/core/oops2/09 Page #3 feel the Technology…
  • 4. Core Java Debasish Pratihari Example : public class Lakshya{ Lakshya(){ System.out.println("Hello"); } Lakshya(int i){ this(); System.out.println("i="+i); } } Arbitrary Number of Arguments : 25%  Note :  Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed. a construct called varargs to pass an arbitrary number of values to a method Example: public class Lakshya{ Lakshya(int...i){ for(int c=0; c<i.length;c++) System.out.println(i[c]); } public static void main(String args[]){ int nums[]={1,2,3,4,5}; Lakshya ob1= new Lakshya(10,20); Lakshya ob2= new Lakshya(10,20,30,40); Lakshya ob3= new Lakshya(nums);  Varargs are treated same as arrays. } } Accessing shadowed Arguments :  A parameter can have the same name as one of the class's fields. If this is the case, the parameter is said to shadow the field.  “this” keyword is used to access the shadowed fields : public class Lakshya{ int a,b Lakshya(int a, int b){ this.a=a; this.b=b; } } Note :  Within an instance method or a constructor, this is a reference to the current object. It is not available to static methods. 4Lecture/core/oops2/09 Page #4 feel the Technology…
  • 5. Core Java Debasish Pratihari Initializing Fields :  Simple initialization can be done inline. class Lakshya { int a=10,b=20; …. }  If initialization involves some kind of logic then constructors are useful.  The facility given by constructors for instance variables can be achieved through static initializers for class variables. Static Initializers:  A static initialization block is a normal block of 25% code enclosed with curly braces and preceded by the static keyword. class Lakshya{ …. satic{ // initialization code here } }  A class can have multiple static block.  Static blocks are executed exactly in the order as they present in the source code. Initializer Blocks :     Java provides an initializer block as an alternative to constructor . Initializer blocks for instance variable are like static initializer blocks, but without the static keyword. The initializer block is copied into all the constructors without a call to this(). The block can be useful to avoid code redundancy from constructors. 5Lecture/core/oops2/09 Page #5 feel the Technology…
  • 6. Core Java Debasish Pratihari Java Objects:      Java object can be created by using the new keyword. They are stored in JVM’s heap area. Java objects are nameless. They can be accessed through their reference only. They are garbage collected if they don’t have any reference. Object & Object Reference 25% Lakshya ob = new Lakshya( ); Note: Heap Area  There is another way to create  objects using 100 ob newInstance() method of Class “Class”. Object reference 100 (Object of Lakshya class) Garbage Collection :     It the process of automatically destroying objects that are no-longer referenced by the program. It is a process for automatic memory management. Basic principles of garbage Collection: o Locate objects that can’t be accessed in future. o Re-claim the resources used by those objects. In java an object may have a finalizer: a method that the garbage collector must run on the object prior to freeing the object. Note :  The Finalizer method in java in finalize().  Finalizers are useful to avoid Resource Leak. 6Lecture/core/oops2/09 Page #6 feel the Technology…