Lecture 13 - Methods
Lecture 13 - Methods
Programming
Lecture 13
Methods
Method?
• 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.
• Write a method once and use it many times.
• No need to write code again and again.
• 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.
• String args[]
o The main() method also accepts some data from the user.
o It accepts a group of strings, which is called a string array.
o It is used to hold the command line arguments in the form of string values.
• In Java, there are two types of methods:
returnType methodName() {
// method body
}
Method
Complete Declaration Syntax
o It is Mandatory in syntax.
o If the method does not return a value, its return type is void.
• methodName
o It is an identifier that is used to refer to the particular method in a program.
o It is Mandatory in syntax.
• method body
o It includes the programming statements that are used to perform some tasks.
• static
o If we use the static keyword, it can be accessed without creating objects.
• parameter1/parameter2
o These are values passed to a method.
1. Instance Method:
• Access the instance data using the object name.
• Declared inside a class.
• Before calling or invoking the instance method, it is necessary to create an object of its class.
Syntax:
void method_name(){
body // instance area
}
Creating a Method
Static Method
2. Static Method
• Access the static data using class name.
• Declared inside class with static keyword.
Syntax:
Signature: square(int a)
Method
Calling a Method
return min;
}
}
public class ExampleMinNumber {
return min;
}
}
Standard Library Methods
• The standard library methods are built-in methods in Java that are readily available
for use.
• These standard libraries come along with the Java Class Library (JCL) in a Java
archive (*.jar) file with JVM and JRE.
Method Overloading
• With method overloading, multiple methods can have the same name with different
parameters:
• Examples:
int addition(int x)
float addition(float x)
}
Advantages of Methods
• Modularity and Reusability:
o Methods promote code modularity by breaking down a program into smaller,
more manageable and reusable parts.
o This makes code maintenance and updates easier.
• Code Organization:
o Methods help in organizing code by encapsulating specific functionality.
o This enhances code readability and makes it easier to understand and maintain.
• Abstraction:
o Methods allow developers to abstract away the implementation details of a task,
providing a high-level view of functionality while hiding the underlying
complexity.
• Parameter Passing:
o Methods can accept parameters, allowing the passing of data and facilitating
customization of behavior.
o This makes methods versatile and adaptable to various scenarios.
• Code Debugging:
o Smaller methods are easier to debug than large, monolithic code blocks.
o Debugging can be more focused and efficient when dealing with well-defined
and isolated methods.
• Encapsulation:
o Methods encapsulate logic, variables, and operations within a defined scope.
o This helps in preventing unintended interference with other parts of the
program.
• Improved Readability:
o Well-named methods enhance code readability by providing clear and
meaningful names for specific tasks or operations.
• Code Maintenance:
o Methods make it easier to maintain and update code. Changes or
enhancements can be localized to specific methods, reducing the risk of
unintended side effects.
• Code Reusability:
o Once a method is written and tested, it can be reused in multiple parts of the
program or even in different projects, saving development time.
END