KIT- KALAIGNARKARUNANIDHI INSTITUTE
OF TECHNOLOGY
M.JAYANTHI
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENE AND ENGINEERING
METHODS IN JAVA
AGENDA
• Introduction
• Method declaration
• Naming a Method
• Types of method
INTRODUCTION
• The method in Java is a collection of instructions that
performs a specific task.
• A method is a block of code or collection of
statements or a set of code grouped together to
perform a certain task or operation.
• It is used to achieve the reusability of code.
• It also provides the easy
modification and readability of code, just by adding
or removing a chunk of code.
• The method is executed only when we call or invoke
it.
Method Declaration
public int sum(int a, int b)
{
//method body
}
public Access specifier
int  return type
sum method name
(int a,int b) parameters
Naming a Method
• Use a name that corresponds to the functionality (if
the method is adding two numbers, use add() or
sum())
• The method name should start with a verb and in
lowercase (Ex: sum(), divide(), area())
• For a multi-word name, the first word should be a
verb followed by a noun or adjective without any
space and with the first letter capitalized
(Ex: addIntegers(), areaOfSquare)
Types of method
• Predefined Method
• User defined Method
User defined Methods
• Static Method
• Instance Method
• Abstract Method
• Factory method
Static Method
• A method that has static keyword is known as
static method.
• The main advantage of a static method is that we
can call it without creating an object.
• It is invoked by using the class name.
• The best example of a static method is
the main() method.
Instance Method
• The method of the class is known as
an instance method.
• It is a non-static method defined in the class.
•
• Before calling or invoking the instance
method, it is necessary to create an object of its
class.
Abstract Methods
• Abstract methods in Java do not have any code in
them. This means that there is no need to provide
the implementation code while declaring it.
• Instead, it is possible to declare the method body
later in the program.
• It is known that one can declare an abstract
method by using the “abstract” keyword.
• There is another hard rule to declare abstract
methods, and it is that they can only be declared
within an abstract class.
Factory Method
• Factory methods are the ones that return an
object to the class where it belongs.
• Usually, all static methods also fall into this
type of method.

METHODS IN JAVA.ppt

  • 1.
    KIT- KALAIGNARKARUNANIDHI INSTITUTE OFTECHNOLOGY M.JAYANTHI ASSISTANT PROFESSOR DEPARTMENT OF COMPUTER SCIENE AND ENGINEERING
  • 2.
  • 3.
    AGENDA • Introduction • Methoddeclaration • Naming a Method • Types of method
  • 4.
    INTRODUCTION • The methodin Java is a collection of instructions that performs a specific task. • A method is a block of code or collection of statements or a set of code grouped together to perform a certain task or operation. • It is used to achieve the reusability of code. • It also provides the easy modification and readability of code, just by adding or removing a chunk of code. • The method is executed only when we call or invoke it.
  • 5.
    Method Declaration public intsum(int a, int b) { //method body } public Access specifier int  return type sum method name (int a,int b) parameters
  • 6.
    Naming a Method •Use a name that corresponds to the functionality (if the method is adding two numbers, use add() or sum()) • The method name should start with a verb and in lowercase (Ex: sum(), divide(), area()) • For a multi-word name, the first word should be a verb followed by a noun or adjective without any space and with the first letter capitalized (Ex: addIntegers(), areaOfSquare)
  • 7.
    Types of method •Predefined Method • User defined Method
  • 8.
    User defined Methods •Static Method • Instance Method • Abstract Method • Factory method
  • 9.
    Static Method • Amethod that has static keyword is known as static method. • The main advantage of a static method is that we can call it without creating an object. • It is invoked by using the class name. • The best example of a static method is the main() method.
  • 10.
    Instance Method • Themethod of the class is known as an instance method. • It is a non-static method defined in the class. • • Before calling or invoking the instance method, it is necessary to create an object of its class.
  • 11.
    Abstract Methods • Abstractmethods in Java do not have any code in them. This means that there is no need to provide the implementation code while declaring it. • Instead, it is possible to declare the method body later in the program. • It is known that one can declare an abstract method by using the “abstract” keyword. • There is another hard rule to declare abstract methods, and it is that they can only be declared within an abstract class.
  • 12.
    Factory Method • Factorymethods are the ones that return an object to the class where it belongs. • Usually, all static methods also fall into this type of method.